Forum Moderators: coopster

Message Too Old, No Replies

PHP and regex question .

         

anshul

11:41 am on Jan 4, 2006 (gmt 0)

10+ Year Member



Hi friends,

I require little help with PHP regex.

I've a whole dump of text:

I wanna count occurence of _L_ in it. I did it easily.

_L_ is 100 times in it.

I wanna get occurence of 'www.example.com' in dump text, if found, stop and return counted value of _L_ like 23rd _L_ or 59th _L_ or 95th _L_

How can this be done using PHP and regex?
There may be more than one occurence of 'www.example.com'

ergophobe

5:27 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't think you can do it with a straight regex. I think you'll need to get the values into an array using preg_match() and then loop through the array for a count. Could be wrong. Something like:


function countLs($subject)
{
$pattern = '/(_L_¦www\.example\.com)/'; // retype the pipe!
$found = preg_match($pattern, $subject, $matches);

if (!$found)
{
return 0;
}

$size = count($matches);
$count = 0;
$continue = true;
while ($count < $size && $continue)
{
if ($matches[$count] == 'www.example.com')
{
$continue = false;
}
else
{
$count++;
}
}

return $count;
}