I have teh string "my car is broken" how can I display the words having a "r"? this script doesn't work : <?php $string="my car is broken"; if ( ereg( "r", $string, $regs ) ) { echo "$regs[1]"; } ?>
$string="my car is broken"; preg_match_all("/\Sr\S/", $string, $matches); echo "<pre>"; print_r($matches); echo "</pre>";
it doesn't work!! I think that it's because of the print_r !!!
Well then, if print_r doesn't work, don't use print_r. Print_r is just a fancy var_dump and isn't necessary to the code. The preg statement does work - I tried it. ProgrammerMan doesn't give out bad code. grin
you mean that the code would be : <?php $string="my car is broken"; preg_match_all("/\Sr\S/", $string, $matches); echo "<pre>"; ?> because if it is, it still doesn't work!
If you're using an older version of PHP that doesn't have print_r, then use var_dump($matches) instead.
.......... didn't you choose a good subject?
it doesn't work too!!!!!
What output do you get in the browser? Any warnings? "it doesn't work" doesn't tell us much about what the problem is.
<?php $string="my car is broken"; preg_match_all("/\Sr\S/", $string, $matches); var_dump($matches); ?>
This works. var_dump($matches); is just to display the contents of the array $matches and you can get rid of it once you understand the array structure.
if you have $string="my car, is broken";
it will match these strings car, broken
if you want it to just match 'car' (no comma), try this:
preg_match_all("/\b\Sr\S\b/", $string, $matches);
p.