Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

return every row that has search term

i want to return every row that has search term

this code only returns the first row that has search term
<?php
$string="
term1 text
Text term2 text
";
preg_match('^.*term.*^',$string,$matches);
echo $matches[0];
?>

Open in new window

Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Modified code below

<?php
$string="
term1 text
Text term2 text
";
preg_match_all('#.*?(term).*?#s',$string,$matches,PREG_OFFSET_CAPTURE);
print_r ($matches);
?>

Open in new window

Avatar of rgb192

ASKER

Array ( [0] => Array ( [0] => Array ( [0] => term [1] => 0 ) [1] => Array ( [0] => 1 text Text term [1] => 6 ) ) [1] => Array ( [0] => Array ( [0] => term [1] => 2 ) [1] => Array ( [0] => term [1] => 19 ) ) )

is the output

can

term1 text
Text term2 text


be output
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks