【发布时间】:2016-05-03 08:58:15
【问题描述】:
我的博客文章有一个功能,访问者可以选择其中的一部分并留下反馈。反馈的主题是选择:它可以是几个词,也可以是一个带有一些 html 标记的短语(不一定围绕它)。
到目前为止,我的功能如下:
function enco_str_replace_nth( $search, $replace, $subject, $nth = 0 ) {
$found = preg_match_all( '/' . preg_quote( $search ) . '/', $subject, $matches, PREG_OFFSET_CAPTURE );
if (false !== $found && $found > $nth) {
return substr_replace( $subject, $replace, $matches[0][$nth][1], strlen( $search ) );
}
return $subject;
}
当没有 HTML 标签时它确实有效。
a completely organic product
但会显示一个警告:preg_match_all(): Unknown modifier,并且在其中包含 HTML 时不起作用。
MSM is a <strong>natural nutrient</strong>
或
because <em>it matters</em> to all of us
知道我应该在这个 preg_match_all 中改变什么吗?
【问题讨论】:
-
只使用其他分隔符(例如
~),问题是/没有转义。
标签: php regex preg-replace preg-match