Page 1 of 1

Using PHP to get all of the anchor text elements in a navbar

Posted: Thu Feb 05, 2009 3:55 pm
by p9142
I am new to PHP, and I am trying to search through an unordered list and grab each of the anchor text elements. I am using this code, but it only returns the first anchor text element, "Link1" Is there a way to expand this code so that I can find all the link elements in my unordered list? Many thanks in advance.

Code: Select all

<?php 
    $links = '<ul><li><a href="http://www.blahblah1.com">Link1</a></li>
               <li><a href="http://www.blahblah2.com">Link2</a></li>
               <li><a href="http://www.blahblah3.com">Link3</a></li>
               <li><a href="http://www.blahblah4.com">Link4</a></li>
               </ul>';
    $matchstring = "/\"\>(.+)\<\/a/";
    preg_match ($matchstring, $links, $match);
    echo $match[1];
?>

Re: Using PHP to get all of the anchor text elements in a navbar

Posted: Thu Feb 05, 2009 4:42 pm
by andym01480
Check out preg_match_all()

Re: Using PHP to get all of the anchor text elements in a navbar

Posted: Thu Feb 05, 2009 4:53 pm
by p9142
got it, thanks very much!

Re: Using PHP to get all of the anchor text elements in a navbar

Posted: Thu Feb 05, 2009 5:05 pm
by RobertGonzalez
You might also want to search greedy matching in regular expressions. That might apply here.