Forum Moderators: coopster

Message Too Old, No Replies

help with preg replace function

         

therealgtron

1:07 am on Feb 7, 2009 (gmt 0)

10+ Year Member



can someone give me a hand with this function?

I'm trying to find a way to ignore punctuation marks and case on the way in but maintain it on the way out.

function bold_keyword($text)
{

$lines = file('custom/seo/keywords');
foreach ($lines as $line)
{
$line = trim($line);
$text = preg_replace('[ '.$line.' ]', ' <strong>'.$line.'</strong> ', $text);
}

return $text;
}

STeeL

2:37 am on Feb 7, 2009 (gmt 0)

10+ Year Member



What exactly do you mean by "ignore on the way in but maintain it on the way out"?

To strip punctuation marks you can use:

$str = str_replace(array('!', '.', ',', ';', ':'), '', $str);

To output everything is lower case:

$str = strtolower($str);

rob7591

3:21 am on Feb 7, 2009 (gmt 0)

10+ Year Member



To ignore case you can use this format in your expression '/pattern/i' the i stands for case-insensitive. In that case you can just do:
preg_replace("/\[($line)\]/i", "<strong>\\1</strong>", $string);

That should do what you want.

therealgtron

4:16 am on Feb 7, 2009 (gmt 0)

10+ Year Member



sorry i wasn't more clear before, steel.

thanks rob, that's pretty close to what i'm looking for.

the only problem is that it replaces it with the case from the list/array.

i want it to just add the <strong> tags and keep the original format.

it should read the list, if it finds the word add <strong> tags.

my original code above makes it so i have to add case variants like the following otherwise it will not hit everything.
Hi Rob
hi rob
Hi rob
hi Rob

your code below eliminates the case variants but replaces it with whatever is in the file.
Hi Rob in the file list will force hi rob to Hi Rob in the returned text.

function bold_keyword($text)
{

$lines = file('custom/seo/keywords.txt');
foreach ($lines as $line)
{
$line = trim($line);
$text = preg_replace("/$line/i", "<strong> $line </strong>", $text);
}

return $text;
}

i hope the above made sense and thanks again for your replies!

rob7591

6:13 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



Edit:

OOOOH i just saw what you're trying to do. gimme a minute i'll try to figure it out.

Edit2:

The first code I sent should have worked.

Use \\1 (the number one) in the replace string (instead of $line). That uses a backreference to see what matched.. it should work:

preg_replace("/$line/i", "<strong>\\1</strong>", $string);

therealgtron

8:47 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



it's weird, when i use below it just blanks out the words it finds.

$string = preg_replace("/$line/i", "<strong>\\1</strong>", $string);

so if the keyword is Rob and the string is "Hi Rob, thank you!" it returns "Hi, thank you!".

i must be screwing something simple up.

therealgtron

10:11 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



i figured it out... had to use $0 instead of \\1

thanks for all your help rob!

rob7591

12:27 am on Feb 8, 2009 (gmt 0)

10+ Year Member



I originally thought it should have been 0 but I double checked with PHP.net and they seemed to be using \\1, oh well, good job figuring it out, happy to help.