Hi,
I have a file contents in a file.
$data=file('myfile.txt');
From this I have to grep for the strings between "Results" and "seconds".
| SitePoint Sponsor |


Hi,
I have a file contents in a file.
$data=file('myfile.txt');
From this I have to grep for the strings between "Results" and "seconds".
Use file_get_contents to load the file into a string. Then use preg_match to search in it, using perl regex syntax.
Here you go.
PHP Code:<?php
foreach(file('somefile.txt') as $sLine)
{
echo (1 === preg_match('~(?<=Results)(.+?)(?=seconds)~', $sLine, $aMatch)) ? $aMatch[0] : '' ;
}
?>![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Hi,
Thanks for the reply.
Can you please tell me how to select only last value i.e 72 from this string
<b>1</b> - <b>10</b> of about <b>72</b>
You could use a regular expression to look for a <b> encapsulated number. Here's an example which takes comma-separated thousands into effect and gives an integer result.
PHP Code:$string = '<b>1</b> - <b>10</b> of about <b>72</b>';
$total = 0;
if (preg_match('#<b>([\d,]+)</b>$#', $string, $match))
{
$total = (int) str_replace(',', '', $match[1]);
}
echo 'There were about ', $total, ' matches.';
Bookmarks