php - preg_replace_callback(): 未知修饰符 '/'
问题描述
我需要搜索并突出显示这个词。
我的句子是
Please see our Author Guide for more information: http://digital-library.theiet.org/journals/author-guide.
you will be contacted shortly asking you to take a decision and sign either a copyright or Open Access licence form.
我的代码
function find_highlight_word($word) {
$text = preg_replace_callback($word, function($matches) use (&$counter) {
$counter++;
return '<b class="search_mark highlighted" id="matched_' . $counter . '">'
. substr($matches[0], 0, strlen($matches[0]))
. '</b>';
}, $text);
return $text;
}
$word = '//';
$word = '/' . preg_quote($word) . '/i';
$this->find_highlight_word($word);
当我用“ // ”搜索时,显示 php 错误。
解决方案
您正确地尝试对字符串进行预引用,但您没有告诉它您的分隔符是什么,因此//
字符串内部会导致问题。将使用的分隔符作为第二个参数传递,因此也可以对其进行转义:
$word = '/' . preg_quote($string, '/') . '/i';
推荐阅读
- java - 引起:java.lang.NumberFormatException:对于输入字符串:“androidx.appcompat.widget.AppCompatEditText
- javascript - 当JS中的链中有原型函数时,有人可以解释链函数是如何工作的吗?
- google-app-engine - 如何模拟“无服务器”云 SQL?
- python - ValueError:模型的特征数量必须与输入匹配。模型 n_features 为 30,输入 n_features 为 2
- python - 网页抓取客户评论
- r - R中的模型公式运算符创建什么数据结构?
- apache-spark - 合并两个包含随时间变化的数据的表(类似于方波卷积)
- python - 尝试使用 Django 应用程序运行 Cron 作业时出现导入错误
- spring - MyBatis 嵌套选择为空白,带有 UUID 输入参数
- ios - 在 map 函数中调用闭包