470,833 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

regex question: replace things ONCE

Dear readers,
i need help on a regular expression:
i have to apply 100 different replaces to an article, but i don't want
that a portion of text already replaced is replaced another time by
another text. the php function strtr() does this but for other reasons
i have to use ereg_replace or preg_replace

to make you understand what i need i paste a portion of the strtr()
function documentation:
-------------------------------------------------------
strtr() will always look for the longest possible match first and will
*NOT* try to replace stuff that it has already worked on.
-------------------------------------------------------


so if i have the following replaces:

[cat]=<a href="#cat">cat</a>
[catalitic] =<a href="#catalitic">catalitic</a>

for the following text= "my cat is catalitic"
so the result should be: my <a href="#cat">cat</ais <a
href="#catalitic">catalitic</a>
and not: <a href="#cat">cat</ais <a href="#<a href="#cat">cat</
a>alitic"><a href="#cat">cat</a>alitic</a>

i can't use strtr() for a reason that's too long to explain

thank you very much! simone
Jun 2 '08 #1
2 1364
Greetings, Giakko.
In reply to Your message dated Tuesday, May 13, 2008, 16:03:28,
so if i have the following replaces:
[cat]=><a href="#cat">cat</a>
[catalitic] =><a href="#catalitic">catalitic</a>
for the following text= "my cat is catalitic"
so the result should be:
my <a href="#cat">cat</ais <a href="#catalitic">catalitic</a>
and not:
<a href="#cat">cat</ais <a href="#<a href="#cat">cat</a>alitic"><a href="#cat">cat</a>alitic</a>
There's a simple way to solve Your problem.
Use regexp modifiers to point that You have the whole word to replace, not a
part of word.
One of them are \b - word boundary.
something like

<?php

$text = 'my cat is catalitic';

$replace = array(
'#\bcat\b#',
'#\bcatalitic\b#'
);

echo preg_replace($replace, '<a href="#$0">$0</a>', $text);

?>
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 2 '08 #2
Giakko wrote:
Dear readers,
i need help on a regular expression:
i have to apply 100 different replaces to an article, but i don't want
that a portion of text already replaced is replaced another time by
another text.
Just do it in one pass. preg_replace() don't check again already
processed part of string.
Jun 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Peter Kleiweg | last post: by
1 post views Thread by Ada | last post: by
17 posts views Thread by clintonG | last post: by
9 posts views Thread by Whitless | last post: by
6 posts views Thread by Martin Evans | last post: by
4 posts views Thread by Morgan Cheng | last post: by
15 posts views Thread by morleyc | last post: by
2 posts views Thread by =?Utf-8?B?TWFya19C?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.