Selecting between two peice of code...

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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Selecting between two peice of code...

Post by Mr Tech »

Let's say this is my code:

Code: Select all

<div>
<p><a href="test1.php">Test 1</a></p>
<p><a href="test2.php">Test 2</a></p>
<p><a href="test3.php">Test 3</a></p>
<p><a href="test4.php">Test 4</a></p>
<p><a href="test5.php">Test 5</a></p>
<p><a href="test6.php">Test 6</a></p>
<p><a href="test7.php">Test 7</a></p>
</div>
I need my PHP script to look through each on of those paragraphs and count how many paragraphs down the test6.php result is. So in this example, it would return the value 6.

Any ideas how I would go about doing this? I have no idea what to do and where to start...
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

One way would be to explode on <p> and iterate through the array with a counter until you find the string with test6.php.

($string = the example code you posted)

Code: Select all

$paragraphs = explode('</p>', $string);

$count = 1;

foreach ( $paragraphs as $paragraph )
{
     if ( strpos($paragraph, 'test6.php') !== false ) // check the manual about strpos...
     {
           // it's $count paragraphs down...
     }
     ++$count;
}
Hope that helps. I can't promise that's the most efficient way of solving your problem, but it should work. :)
Post Reply