Page 1 of 1

preg_replace won't change data

Posted: Sun Dec 05, 2010 10:01 am
by someguyhere
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.

Code: Select all

		$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/"

Re: preg_replace won't change data

Posted: Sun Dec 05, 2010 11:45 am
by cpetercarter
You need to change two things.

First, php will recognise variables inside double quoted strings, but not single quoted strings.

Second, you need the delimiters (/) for the beginning and the end of the pattern you are searching for only for the first parameter, not for the second.

So, the following should work.

Code: Select all

paragraph1 = preg_replace("/ $keyword_phrase /", " $in_content_link ", $paragraph1, 1);

Re: preg_replace won't change data

Posted: Sun Dec 05, 2010 8:14 pm
by someguyhere
Thank you