我正在使用以下内容:
$pattern_array = array();
$replace_array = array();
$pattern_array[] = '#\[vc_row][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel#i';
$replace_array[] = '[vc_row fixed_width="true"][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel';
echo preg_replace($pattern_array, $replace_array, $content);
要替换此文本:
[vc_row][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel
有了这段文字:
[vc_row fixed_width="true"][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel
但我收到错误:
preg_replace(): Compilation failed: range out of order in character class at offset 69
我不需要任何正则表达式规则或任何东西,我只想用另一个文本块替换一个文本块,而忽略所有 [方括号] 和“引号”,有没有办法用 preg_replace 做到这一点?
附带说明一下,我知道我可以使用 str_replace,但是我将它添加到一个循环遍历数组并且已经使用 preg_replace 的函数中,因此 str_replace 不是一个选项。
您只需要转义正则表达式中使用的特殊字符。有一个很棒的函数被调用preg_quote
,它会自动完成。
$pattern_array = array();
$replace_array = array();
$pattern_array[] = '#' . preg_quote('[vc_row][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel', '#') . '#i';
$replace_array[] = '[vc_row fixed_width="true"][vc_column width="1/2" css=".vc_custom_1503896034143{margin-bottom: 40px !important;}"][shortcode_testimonials_carousel';
echo preg_replace($pattern_array, $replace_array, $content);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句