homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

need help in filtering text

 2:18 am on Apr 18, 2003 (gmt 0)

Ok. first here an overview of what I would like to accomplish:

a string of text and change the word/s listed there into links if the words are found from the list.

example: text = "foo is foober". list = "foo, foober"

the word "foo" and "foober" should change into a link.

using str_replace it would change "foo" in "foo" and "foo" in "foober" into links.

Anyone can shed light on this? I am currently testing the preg_replace and experiencing some problems.




 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?


 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!


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


 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>");


 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(
// ... 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]


 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!) ;)


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


 3:28 am on Apr 18, 2003 (gmt 0)

Regular expressions can be tricky to master, but oh so useful ;)


 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.


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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved