3

I read this PHP RegEx page, but either I'm missing something, misreading something, or it doesn't work the way they say. I'm guessing it's one of the first two.

$str = preg_replace("([|]\d*)", "\1;", $str);

1 Answer 1

4

Your regular expression should follow Perl syntax, meaning it has to start and end with the same character (with some exceptions). Also, the back reference should start with a double slash, to get around PHPs double escaping. This should work (with a quick test):

$str = "asdfasdf |123123 asdf iakds |302 asdf |11";
$str = preg_replace("/([|]\d*)/", "\\1;", $str);
echo $str; // prints "asdfasdf |123123; asdf iakds |302; asdf |11;"
1
  • Note that the $n backreference syntax is preferred over \\n. So says the manual.
    – Geert
    Jul 9, 2009 at 8:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.