preg_replace won't change data

jlknauff

New member
Aug 25, 2008
237
1
0
The preg_replace I'm using isn't working. Basically, it's supposed to take a particular word within the content, and wrap a link to and internal page around the word. The spaces on each side of each variable are so that it only links the actual word, not a coincidental occurrence of the letters within another word.

PHP:
        $paragraph1 = preg_replace('/ $keyword_phrase /', '/ $in_content_link /', $paragraph1, 1);

If it makes a difference, the variables will all contain strings, so

$keyword_phrase might be something like "Contact Us "

$in_content_link might be something like "/contact-us/" or "http://www.domain.com/contact-us/"
 


Code:
$p = preg_replace("/$keyword_phrase/", $in_content_link, $paragraph1, 1);
two changes are needed in your syntax -- $variables won't get parsed inside single quote strings (i changed '/$keyword_phrase/' to double quotes) and the second argument should just be your variable (not surrounded by forward-slashes). also, you probably don't want those spaces around "$keyword_phrase".