Reply to Re: replace all occurrences of a substring in a string (RE)

Your name:

Reply:


Posted by Jim Michaels on 03/02/06 00:46

"Jasen Betts" <jasen@free.net.nz> wrote in message
news:666c.43f6679c.8f2d2@clunker.homenet...
> On 2006-02-17, Piratron <piratron05@gmail.com> wrote:
>> Jasen Betts wrote:
>>> On 2006-02-16, Piratron <piratron05@gmail.com> wrote:
>>>> MaKroZ wrote:
>>>>> On Mon, 13 Feb 2006 19:09:33 +0100, Piratron wrote:
>>>>>
>>>
>>>> <tr><td>
>>>> <!--ec1-->text1<!--ec2-->
>>>> </td></tr>
>>>> <tr><td>
>>>> <!--ec1-->text2<!--ec2-->
>>>> </td></tr>
>>>> <tr><td>
>>>> <!--ec1-->text3<!--ec2-->
>>>> </td></tr>
>>>> <tr><td>
>>>> <!--ec1-->text4<!--ec2-->
>>>> </td></tr>
>>>> ---------------------
>>>>
>>>> the problem is that
>>>> ereg_replace("(<!--ec1-->).*(<!--ec2-->)","\\1$repl\\2", $string);
>>>> will replace text from first "<!--ec1-->" occurrence (line 2 in
>>>> example)
>>>> and last occurrence (line 12 in example).
>>>
>>> you need a non-greedy match.
>>
>> ehmm, and how to make it a non-greedy?
>
> hmm, you have to use perl compatible regex for that. put a U "flag" at
> the end of the regex string.

if that makes it Perl compatible, then you should modify your * to be .*? to
make it non-greedy.
funny, they were just talking about this in the previous thread. :-)

preg_replace("/(<!--ec1-->).*?(<!--ec2-->)/U","\\1$repl\\2", $string);

>
> preg_replace("/(<!--ec1-->).*(<!--ec2-->)/U","\\1$repl\\2", $string);
>
> --
>
> Bye.
> Jasen

[Back to original 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

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