You are here: Re: replace all occurrences of a substring in a string (RE) « All PHP « IT news, forums, messages
Re: replace all occurrences of a substring in a string (RE)

Posted by Piratron on 02/16/06 17:55

MaKroZ wrote:
> On Mon, 13 Feb 2006 19:09:33 +0100, Piratron wrote:
>
>> well, I'm afraid you didn't understand me. Simple str_replace function
>> wont solve my problem because I have to replace text between two html tags!
>> I can determine the position of first an last tag with a loop or
>> something and then replace the text inside it. But I have a dozens of
>> those tags so I really need a smart function to find all that
>> occurrences. What will be the faster method? The one I just mentioned or
>> using eregi_replace or preg_replace?
>
> Oh, so '{POST_CODE}' is not the pattern? OK, sorry I didn't understand you
> right. Seeking for positions of tags in a loop is a good approach too. I
> don't know which of approaches will work faster, but it can be determined
> by an experiment though. If you want to try re, here is an example for
> your problem:
>
> ereg_replace("(<!--ec1-->).*(<!--ec2-->)","\\1$repl\\2", $string);
>
> "This function scans string for matches to pattern, then replaces the
> matched text with replacement. The modified string is returned."
>
> eregi_replace is the same but it's case insensitive.
>
>
>

hey,
I just noticed that this is actually not working very well...
Here is the example:

---------------------
<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).

I want to replace text between two neighbor tags. In another words to
replace text1 on second line, then text2 on fifth line and so on...

PS: I have added "<tr><td>" just for an example, so that is not a
pattern. It can be various html code.

Thank you in advance

--
Please visit our site at:
www.symbian-mobile.org

Regards,
Symbian-Mobile Team

 

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

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