You are here: Re: Preg_match not always returning first match? « All PHP « IT news, forums, messages
Re: Preg_match not always returning first match?

Posted by Juliette on 08/23/06 07:14

FFMG wrote:
> Hi,
>
> If I have a string:
> $s = "sometext more_text hello1hello2";
>
> And I want to look for the text 'hello', I do the following regex
> search
> /hello([0-9]*)/is
> And I get the expected value of '1', (the first hello)
>
> But if I do
> /.*hello([0-9]*)/is
> I get the next 'hello', ('2').
>
> But, because I want find the first 'hello' _after_ 'sometext', I want,
> (need?), to do
>
> /sometext.*hello([0-9]*)/is
>
> But that returns the second 'hello' not the first one.
>
> What could I be doing wrong?
>
> Thanks
>
> FFMG
>


You are using the . which is a 'catch-all' and is greedy (i.e. will
'eat' as much text as it possibly can).

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация