strip_tags() removes HTML from the value of a variable. The second parameter is useful if you want to make exceptions and leave specific tags, for example p aragraph tag.
$text = '<p>Paragraph.</p> <a href="#">Other text</a>'; echo strip_tags($text); // Paragraph. Other text echo strip_tags($text, '<p><a>'); // <p>Paragraph.</p> <a href="#">Other text</a>
phpQuery
If you want to stay away from regular expressions, you can use phpQuery to process the value, and then use the jQuery style of selectors and methods to get your value:
// Bring in phpQuery require("phpQuery-onefile.php"); // Load up our HTML phpQuery::newDocumentHTML("<a href='http://sampsonresume.com/'>Homepage</a>"); // Print the HREF attribute of the first Anchor print pq("a:first")->attr("href"); // http://sampsonresume.com/
Regex
To find the url you can use the following:
$var = "<a href='http://sampsonresume.com/'>Homepage</a>"; preg_match("(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)",$var,$match); print $match[0];
source share