php PHP使用preg_replace:“分隔符不能是字母数字或反斜杠”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2527657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:50:58  来源:igfitidea点击:

PHP using preg_replace : "Delimiter must not be alphanumeric or backslash" error

phpregexpreg-replace

提问by Christopher

I am trying to take a string of text like so:

我正在尝试像这样获取一串文本:

$string = "This (1) is (2) my (3) example (4) text";

In every instance where there is a positive integer inside of parentheses, I'd like to replace that with simply the integer itself.

在括号内有正整数的每种情况下,我想用简单的整数本身替换它。

The code I'm using now is:

我现在使用的代码是:

$result = preg_replace("\((\d+)\)", "
$result = preg_replace("((\d+))", "
$result = preg_replace("#\((\d+)\)#", "", $string);
", $string);
", $string);

But I keep getting a

但我不断收到

Delimiter must not be alphanumeric or backslash.

分隔符不能是字母数字或反斜杠。

Warning

警告

Any thoughts? I know there are other questions on here that sort of answer the question, but my knowledge of regex is not enough to switch it over to this example.

有什么想法吗?我知道这里还有其他问题可以回答这个问题,但我对正则表达式的了解不足以将其切换到这个例子。

回答by codaddict

You are almost there. You are using:

你快到了。您正在使用:

$result = preg_replace("/\((\d+)\)/", "\1", $string);
  • The regex you specify as the 1st argument to preg_*family of function should be delimited in pair of delimiters. Since you are not using any delimiters you get that error.
  • (and )are meta char in a regex, meaning they have special meaning. Since you want to match literal open parenthesis and close parenthesis, you need to escape them using a \. Anything following \is treated literally.
  • You can capturing the integer correctly using \d+. But the captured integer will be in $1and not $0. $0will have the entire match, that is integer within parenthesis.
  • 您指定为preg_*函数族的第一个参数的正则表达式应以分隔符对分隔。由于您没有使用任何分隔符,因此您会收到该错误。
  • (并且)是正则表达式中的元字符,这意味着它们具有特殊含义。由于您想匹配文字左括号和右括号,您需要使用\. 以下任何内容\均按字面处理。
  • 您可以使用\d+. 但捕获的整数将在$1而不是$0$0将有整个匹配,即括号内的整数。

If you do all the above changes you'll get:

如果您进行上述所有更改,您将获得:

<?php
$string = "This (1) is (2) my (3) example (4) text";
$output = preg_replace('/\((\d)\)/i', '', $string);
echo $output;
?>

回答by animuson

1) You need to have a delimiter, the /works fine.

1)你需要有一个分隔符,/工作正常。

2) You have to escape the (and )characters so it doesn't think it's another grouping.

2)你必须转义()字符,所以它不认为这是另一个分组。

3) Also, the replace variables here start at 1, not 0 (0 contains the FULL text match, which would include the parentheses).

3) 此外,这里的替换变量从 1 开始,而不是 0(0 包含完整文本匹配,其中包括括号)。

$yourString='hi there, good friend';
$dividorString='there';
$someSstring=preg_replace("/$dividorString/",'', $yourString);

echo($someSstring);

// hi, good friend

Something like this should work. Any further questions, go to PHP's preg_replace()documentation- it really is good.

像这样的事情应该有效。任何进一步的问题,请转到PHP 的preg_replace()文档- 这真的很好。

回答by pix0r

Check the docs - you need to use a delimiter before and after your pattern: "/\((\d+)\)/"

检查文档 - 您需要在模式前后使用分隔符: "/\((\d+)\)/"

You'll also want to escape the outer parentheses above as they are literals, not a nested matching group.

您还需要转义上面的外括号,因为它们是文字,而不是嵌套的匹配组。

See: preg_replace manual page

请参阅:preg_replace 手册页

回答by Atli

Try:

尝试:

##代码##

The parenthesis chars are special chars in a regular expression. You need to escape them to use them.

括号字符是正则表达式中的特殊字符。你需要逃避它们才能使用它们。

回答by Darian Harrison

Delimiter must not be alphanumeric or backslash.,

分隔符不能是字母数字或反斜杠。,

try typing your parameters inside "/ .... /" as shown bellow. Else the code will output >>> Delimiter must not be alphanumeric or backslash.

尝试在“/ .... /”中输入参数,如下所示。否则代码将输出 >>> 分隔符不能是字母数字或反斜杠。

##代码##

. . worked for me.

. . 为我工作。