Page 1 of 1

Selecting between two peice of code...

Posted: Mon Nov 14, 2005 7:57 pm
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...

Posted: Mon Nov 14, 2005 8:05 pm
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. :)