Closed Thread Icon

Topic awaiting preservation: preg_replace delimiter error (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12446" title="Pages that link to Topic awaiting preservation: preg_replace delimiter error (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: preg_replace delimiter error <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 09-24-2002 16:35

I am getting a Delimiter must not be alphanumeric or backslash in the line that actually does the preg_replace in this code:

code:
$body = "Today, Pugzly decided he wanted a helicopter. So, Pugzly went out and bought one";
$toreplace = "Pugzly";
$replacewith = "<a href=\"/article.php?aid=1\">Pugzly</a>";
echo $body . "<br />\n";
echo $toreplace . "<br />\n";
echo $replacewith . "<br />\n";
$body = preg_replace ($toreplace, $replacewith, $body, 1);
echo $body;



Everything works up to that line. I'm not sure what the error means. I've tried hardcoding the $toreplace and $replacewith text right into the preg_replace call, but that didn't work either. What am I doing wrong?



[This message has been edited by Pugzly (edited 09-24-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-24-2002 16:40

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 09-24-2002 17:11

Magic quotes are on.

I changed the $replacewith to
$replacewith = "Mr. Pugzly";
just to remove the theory about the slashes. No help.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-24-2002 17:30

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-24-2002 17:55

Pugzly, $replacewith variable doesn't containt valid regular expression and that's why preg_replace() function fails. Since you only need to perform simple search & replace operation use str_replace() function instead...


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 09-24-2002 17:56

Ini's right about str_replace.

str_replace is faster than preg_replace if you don't really need the power of a regexp.

And to make your code work with preg the way it's written Pugzly the one line needs to be changed to look like this.

$toreplace = "/Pugzly/";

[edit]

As usual mr.maX beat me to it.

Oh well!

[/edit]

-Butcher-

[This message has been edited by butcher (edited 09-24-2002).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 09-24-2002 18:19

Actually, str_replace won't do what I need it to do. I only need it to replace the FIRST instance of $toreplace.

Emperor came up with a solution:

code:
<?php

$input = "One day $guru was walking down the road and saw a cat so $guru took it home.";

$search = array ("'Pugzly'i",
"'Emperor'i",
"'Steve'i",
"'DL'i");

$replace = array ("Overlord",
"Code Monkey",
"Flasher",
"Visual Diva");

$output = preg_replace ($search, $replace, $input, 1);

echo $input;

echo '<br />';

echo $output;

?>



and this works fine.



[This message has been edited by Pugzly (edited 09-24-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu