tags:

views:

68

answers:

4

What's the best way to extract HTML out of $var?

example of $var

$var = "<a href="http://stackoverflow.com/"&gt;Stack Overflow</a>"

I want

$var2 = "http://stackoverflow.com/"

example: preg_match();

what else?

+1  A: 

strip_tags() removes HTML from the value of a variable. The second parameter is useful if you would like to make exceptions, and leave certain tags in, like the paragraph tag.

$text = '<p>Paragraph.</p> <!-- boo --> <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 could use phpQuery to handle the value, and then use jQuery-style 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/'&gt;Homepage&lt;/a&gt;");
// Print the HREF attribute of the first Anchor
print pq("a:first")->attr("href"); // http://sampsonresume.com/

Regex

You can use the following to find the URL:

$var = "<a href='http://sampsonresume.com/'&gt;Homepage&lt;/a&gt;";
preg_match("(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)",$var,$match);
print $match[0]; // http://sampsonresume.com/
Jonathan Sampson
right, what about html tag? I want that not a anchor
alexus
You want only HTML and everything inside?
Jonathan Sampson
I want $var2 to contain URL from href link (I want content of href link)
alexus
what non-regex way this can be accomplished?
alexus
Alexus, which phpQuery. I've updated my answer. I'll get you an example.
Jonathan Sampson
A: 

Use the following regular expression:

\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))
Alix Axel
other then regex aka preg_match();
alexus
@alexus: What? I don't understand...
Alix Axel
Alix, I think he wants to stay away from expressions :(
Jonathan Sampson
what non-regex way this can be accomplished?
alexus
Alexus, look at my updated answer regarding phpQuery.
Jonathan Sampson
yes, non regex that's why i gave this as an example already
alexus
+1  A: 

Instead of crafting long complicated regex, do it in steps

$str = '<a href="http://stackoverflow.com/"&gt; 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/"&gt; Stack Overflow</a>';
$s = explode('href="',$str);
$t = explode('">',$s[1]);
print $t[0];
ghostdog74
what non-regex way this can be accomplished?
alexus
awesome just what i was looking for.. is there a way to put that into one line though? explode part? or is there another way that you don't need any variables at all? like sort of echo (function(function())); and i'd get output i'm seeking for?
alexus
not that i know. maybe someone else can help you better
ghostdog74
+1  A: 

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/"&gt;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/ 
zombat