Thx Erik, it works great except for one detail. It matches numbers too.
Can you fix it so it matches only words please ?

For example:
$file = 'this is a number 1231 test.';
$words = 'test|number';
Result: [tag]this[/tag] [tag]is[/tag] [tag]a[/tag] [tag]nu[/tag]
[sample]number[/sample] [tag]1231[/tag] [sample]test[/sample].

1231 should stay the same, without tags.

thx

<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hello Gregor,
>
> On 13 Jan 2004 at 15:08, Gregor Jaksa wrote:
>
> > hello, i need to replace some words in file with [tag]word[/tag] and
other
> > words with [sample]word[/sample].
> >
> > i have a list of words which i need to replace with [sample] tags
declared
> > like
> > $words = 'word1|word2|word3|word4';
> >
> > replacing those words is easy i just use preg_replace('/\b( ' . $words .
> > ')\b/i', '[sample]$1[/sample]', $file);
> > but how to replace all other words that are not in $words array with
[tag]
> > tag ?
> >
> > thx in advance!
>
> Let me see if I understood you correctly: you want to surround all the
words in the string
> $file that are not also contained in the $words array with [tag] and
[/tag]?
>
> Here's an idea:
>
> <?php
>
> $file = 'this is my test string and I just want to use it for a quick
test';
> $words = 'test|string';
>
> $new_file = preg_replace('/\b('.$words.')\b/', ' [sample]$1[/sample] ',
$file);
>
> $new_file =
preg_replace('/\b(?<!\[sample\]|\[)(\w+)(?!\[\/sample\]|\])\b/',
>      ' [tag]$1[/tag] ', $new_file);
>
> print $new_file;
>
> ?>
>
> Very important: you *must not* forget the spaces before and after the
opening and
> closing brackets in the replacement string.
>
> Good luck,
>
> Erik

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to