DrDoc

msg:1247459 | 2:33 am on Apr 18, 2003 (gmt 0) |
$string = "foo is foobar"; $word = "foo"; $string = preg_replace("/($word)/","<a href=\"$1.html\">$1</a>",$string); This will replace all occurances of "foo" with a link to "foo.html". Is that what you were looking for?
|
copongcopong

msg:1247460 | 2:39 am on Apr 18, 2003 (gmt 0) |
sort of ... the list would contain numerous words to filter, let me try that and add some loops ... thanks!
|
DrDoc

msg:1247461 | 2:41 am on Apr 18, 2003 (gmt 0) |
If you have a list of words, this would work: $string = "foo is foobar, baz is widgets"; $wordlist = array("/(foo(?!bar))/","/(foobar)/","/(widgets)/"); $string = preg_replace($wordlist,"<a href=\"$1.html\">$1</a>"); This will replace each word in the list with a link to a page with the same name. Not that the first word will only match "foo" as long as it's not followed by "bar". This will ensure that foo in foobar isn't linked. It is also possible to use "\b" before and after each word. It will then only match whole words. $wordlist = array("/(\bfoo\b)/","/(\bfoobar\b)/","/(\bwidgets\b)/"); The parenthesis lets you use the matched word in the replacement string...
|
DrDoc

msg:1247462 | 2:46 am on Apr 18, 2003 (gmt 0) |
If you want to perform case insensitive matching, this would work best: $string = "foo is foobar, baz is widgets"; $wordlist = array("/(\bfoo\b)/i","/(\bfoobar\b)/i","/(\bwidgets\b)/i"); $string = preg_replace($wordlist,"<a href=\"".strtolower($1).".html\">$1</a>");
|
mischief

msg:1247463 | 3:15 am on Apr 18, 2003 (gmt 0) |
You can use arrays as arguments to preg_replace() to specify regexes and replacements. This is (I think) a less efficient method than suggested by DrDoc, but it might be easier to fiddle with later on (for someone like me at least). In your case, you would only need an array for the first argument to preg_replace() with a list of words you want removed. So, you could do something like this: <? $string = 'one two three';$tolinks = array( '/\b(one)\b/i', '/\b(three)\b/i', // ... etc... ); $string = preg_replace($tolinks, '<a href="$1">$1</a>', $string); ?>
That would leave $string as '<a href="one">one</a> two <a href="three">three</a>'. If it was a really big list of words to be linkerised, maybe you could also do something like make a simple array of words, then loop through it and add the "/\b...\b/i" bits... [ edit: added code style tags. didn't seem to do much in the preview? :) ] [edited by: mischief at 3:23 am (utc) on April 18, 2003]
|
DrDoc

msg:1247464 | 3:22 am on Apr 18, 2003 (gmt 0) |
I just noticed a mistake in my example... The \b should of course not be included inside the parenthesis. :) mischief's example doesn't, which is the correct way... Otherwise the word delimited would be included in the link (oops!) ;)
|
copongcopong

msg:1247465 | 3:23 am on Apr 18, 2003 (gmt 0) |
Thanks again ... i'll try to learn more on this ... im still a stranger to regex ... i'll be back to ask again. Thanks. :)
|
DrDoc

msg:1247466 | 3:28 am on Apr 18, 2003 (gmt 0) |
Regular expressions can be tricky to master, but oh so useful ;)
|
mischief

msg:1247467 | 3:31 am on Apr 18, 2003 (gmt 0) |
It doesn't actually matter. :-) \b doesn't go into $1, because technically it matches just before the word boundary, not the boundary itself.
|
copongcopong

msg:1247468 | 4:07 am on Apr 18, 2003 (gmt 0) |
its working but i am receiving an error message: Warning: Empty regular expression in path/path/ line xx anyone? Thanks to all. :)
|
|