在preg_replace-PHP中处理字符/字符

在preg_replace-PHP中处理字符/字符,php,regex,preg-replace,Php,Regex,Preg Replace,我想替换字符串(或段落)中出现的所有特定术语 这些术语可以是蓝色餐厅海洋/蓝色餐厅,蓝色餐厅 $inputext = "The name is Ocean/Blue Restaurant "; $term = "Ocean/Blue Restaurant"; $rule = "/". $term."/i"; $replacetext = "[X]"; $outputext = preg_replace($rule, $replacetext, $inputext); echo $outpute

我想替换字符串(或段落)中出现的所有特定术语

这些术语可以是蓝色餐厅海洋/蓝色餐厅,蓝色餐厅

$inputext = "The name is Ocean/Blue Restaurant "; 
$term = "Ocean/Blue Restaurant";
$rule = "/". $term."/i";
$replacetext = "[X]";
$outputext = preg_replace($rule, $replacetext, $inputext);
echo $outputext,"\n";
所需输出

 The name is [X]
当输入中有字符“/”时,它无法工作

我的主要目标是替换段落中出现的所有短语。是否有使用preg_替换的通用方法


谢谢您的帮助。

正在使用exmample进行转义


通过使用进行转义的工作示例


代码的问题在于,应该转义在正则表达式中用作分隔符的字符。此外,您不需要
preg.*
函数。您所需要的只是:

输出:

The name is [x] [x] [x]

代码的问题在于,应该转义在正则表达式中用作分隔符的字符。此外,您不需要
preg.*
函数。您所需要的只是:

输出:

The name is [x] [x] [x]

看起来你在寻找你的案子想要的结果是什么?我猜是一个错误输入:term必须是$term in:$rule=“/”中的$term。术语“/i”@感谢您的反馈。更新的问题看起来你在寻找你的案例中想要的结果是什么?我猜是一个错误输入:term必须是$term in:$rule=“/”中的$term。术语“/i”@感谢您的反馈。更新的问题$inputext由用户输入输入,因此没有使用Escape的句柄,如果输入是Ocean#Blue Restaurant怎么办?我更新了答案,现在,它可以处理用户传递的数据,您还可以轻松地在future@Casimiret Hippolyte true它不是:)添加了更多干净的代码示例,建议$inputext由用户输入提供,因此没有使用Escape的句柄,如果输入是Ocean#Blue Restaurant怎么办?我更新了答案,现在,它可以处理用户传递的数据,您还可以轻松地在future@Casimiret Hippolyte true它不是:)根据建议添加了更多干净的代码示例
$str = "The name is Ocean/Blue Restaurant Blue Restaurant Blue's Restaurant"; 
echo strtr($str, ["Blue Restaurant" => "[x]", "Ocean/Blue Restaurant" => "[x]", "Blue's Restaurant" => "[x]"]);
The name is [x] [x] [x]