What's the best way to extract HTML out of $var?
example of $var
$var = "<a href="http://stackoverflow.com/">Stack Overflow</a>"
I want
$var2 = "http://stackoverflow.com/"
example: preg_match();
what else?
Instead of crafting long complicated regex, do it in steps
$str = '<a href="http://stackoverflow.com/"> Stack Overflow</a>';
$str = preg_replace("/.*<a\s+href=\"/","",$str);
print preg_replace("/\">.*/","",$str);
one way of "non regex", using explode
$str = '<a href="http://stackoverflow.com/"> Stack Overflow</a>';
$s = explode('href="',$str);
$t = explode('">',$s[1]);
print $t[0];
If it's a valid HTML string that you have, then the DOMDocument module's loadHTML() function will work, and you can navigate your structure very easily. This is a good way to do it if you have a lot of HTML to work with.
$doc = new DOMDocument();
$doc->loadHTML('<a href="http://stackoverflow.com/">Stack Overflow</a>');
$anchors = $doc->getElementsByTagName('a');
foreach($anchors as $node) {
echo $node->textContent;
if ($node->hasAttributes()) {
foreach($node->attributes as $a) {
echo ' | '.$a->name.': '.$a->value;
}
}
}
produces the following:
Stack Overflow | href: http://stackoverflow.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With