0
votes

I'am having trouble with a preg_replace() and I have tried to solve it for some time now. I'am getting this error:

preg_replace: Delimiter must not be alphanumeric or backslash

Here is my code:

$id = 23;
$titleForUrl = "I_like_cookies";
$uploadShortDesc = "This is the best desc and I love it 1242";

$uploadShortDesc = preg_replace('\1242\',
    '<a href="http:\/\/google.com\/freecookies\/'.$id.'-'.$titleForUrl.'">http:\/\/google.com\/freecookies\/'.$id.'-'.$titleForUrl.'.<\/a> ',
    $uploadShortDesc);
echo $uploadShortDesc;

Is should echo:

This is the best desc and I love it <a href="http://google.com/freecookies/23-  I_like_cookies">http://google.com/freecookies/23-I_like_cookies.</a>

I would love if someone could help me, I have put a \ in front of all / but I don't know why it won't work.

1
preg_replace('/1242/', read the manual php.net/manual/en/function.preg-replace.php - Plus, why the \/ etc.? - Funk Forty Niner
Thanks mate, you're awesome! - Joakim Johansson
Btw, you asked many questions but accepted none. Visit meta.stackexchange.com/a/5235 then return to the answer(s) given and tick the checkmark as shown. This will mark the question(s) as solved. That's the way Stack rolls ;-) - Funk Forty Niner
Is it possible to mark an comment as answer? - Joakim Johansson
No it isn't. Would be nice though ;-) - Funk Forty Niner

1 Answers

0
votes

Thanks Fred -ii- for the help!

it should look like:

$uploadShortDesc = preg_replace('\1242\', '<a href="http://google.com/freecookies/'.$id.'-'.$titleForUrl.'">http://google.com/freecookies/'.$id.'-'.$titleForUrl.'.</a> ', $uploadShortDesc);

Thanks! I really should read that manual more...