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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p9142
Forum Newbie
Posts: 2
Joined: Tue Feb 03, 2009 1:46 pm

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

Post 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];
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

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

Post by andym01480 »

Check out preg_match_all()
p9142
Forum Newbie
Posts: 2
Joined: Tue Feb 03, 2009 1:46 pm

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

Post by p9142 »

got it, thanks very much!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post by RobertGonzalez »

You might also want to search greedy matching in regular expressions. That might apply here.
Post Reply