Forum Moderators: coopster

Message Too Old, No Replies

Extracting multiple multiple matches using regex

         

ukgimp

2:41 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have used simple regex for extracting URLs from a page or string of text, but it would be useful to get both the URL and the text within the A ref tag

So for just the URL only:
$contents = "some string with a <a ref="http://www.WebmasterWorld.com">WebmasterWorld</w>";

preg_match_all("/href=\"(.*?)\"/", $contents, $link_results);

How do you go about getting both the url and the WebmasterWorld and put them into an array so that if you wanted you could print_r the whole result set of there were more than one.

does that make sense, i hope so :)

I reckon this may have been covered before, but no joy as far as looking.

mincklerstraat

3:48 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can try:

preg_match_all('#<a\s+href\s*=\s*[\'¦"]([^\'"]*)[\'"][^>]*>([^<]*)<#', $contents, $linkresults);
echo '<pre>'.htmlspecialchars(print_r($linkresults, TRUE)).'</pre>';

Just tried this on the 'active posts' source and it choked up since there were other tags between <a href...> and </a>, but if it's just simple links like you mention, this looks like it works to me.