Welcome to WebmasterWorld Guest from 54.242.134.77

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg_replace

need help in filtering text

     

copongcopong

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

10+ Year Member



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.

Thanks.

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$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

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

10+ Year Member



sort of ... the list would contain numerous words to filter, let me try that and add some loops ... thanks!

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

mischief

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

10+ Year Member



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

4:07 am on Apr 18, 2003 (gmt 0)

10+ Year Member



its working but i am receiving an error message:

Warning: Empty regular expression in path/path/ line xx

anyone? Thanks to all. :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month