Page 1 of 1

preg_replace problem

Posted: Wed Dec 30, 2009 4:11 pm
by someguyhere

Code: Select all

function addLinks(){
 
    preg_replace("text", "<a href=\"http://www.example.com/\">text</a>", $spun, 1);
 
}
Gives me the error message "Delimiter must not be alphanumeric or backslash" - what am I doing wrong here? If it makes a difference, this function goes through a chunk of text and replaces certain words with a hyperlinked version of itself.

Re: preg_replace problem

Posted: Wed Dec 30, 2009 4:56 pm
by cpetercarter
The first argument is the pattern to search for. The pattern needs 'delimiters' at the beginning and the end. Commonly a forward slash (/) is used as a delimiter. So - "/text/" , not "text".

Re: preg_replace problem

Posted: Wed Dec 30, 2009 5:30 pm
by requinix
If you don't want regular expressions then don't use preg_replace.

str_replace

Re: preg_replace problem

Posted: Wed Dec 30, 2009 5:51 pm
by someguyhere
Ok, perhaps I'm doing something else wrong then...I don't get the error message now that I've made that change, but I'm getting no output. When I do it outside of a function, it works fine. Do you see any mistakes here? (by the way, I'm going to drop preg_replace and use str_replace instead)

Code: Select all

 
 
function addLinks(){
 
 preg_replace("/text/", "/<a href=\"http://www.example.com/\">text</a>/", $spun, 1);
 
}
 
 $spun = spinContent($spin_this);
 
 $post = addLinks($spun);
 
 echo $post
 
 

Re: preg_replace problem

Posted: Wed Dec 30, 2009 5:56 pm
by daedalus__
although it isn't required all functions should return a value. the problem is your function is not returning the result of the preg_replace.

Code: Select all

 
function awful_dodger($data)
{
      return modify($data);
}
 
understand?