I have a long text which contains:
city_nameLAcity_name some text here and there ;) city_nameLondoncity_name some other text will goes here too then city_nameTehrancity_name and of course some other junk text here.
I want to slurp LA, London, and Tehran from the text above. I ereg, but couldn’t get what I want I used something like below:
ereg ("city_name(.*)city_name", $longText, $output);
How to do this job? I appreciate your inputs!
Use
(.*?)instead of(.*). The difference is that.*will match as much as possible (using the greedy quantifier*) while.*?will match as little as possible (*?is a lazy quantifier). So unless you have a city whose name includescity_namesomewhere (dunno, the US has some strange names; maybe it happens).*?should get you what you want.Random note: This might not work with
ereg, I’m not intimately familiar with extended POSIX regular expressions right now. However, those functions are deprecated in PHP for a while now (and you should get a warning about that, too). It’s better to replace them by the variouspreg_*functions where possible. The substitute would bepreg_match_all, I believe.