preg_replace problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

preg_replace problem

Post 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.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: preg_replace problem

Post 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".
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: preg_replace problem

Post by requinix »

If you don't want regular expressions then don't use preg_replace.

str_replace
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: preg_replace problem

Post 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
 
 
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: preg_replace problem

Post 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?
Post Reply