简体   繁体   English

php preg_replace,字符串包含|,[,]符号

[英]php preg_replace with strings containing |, [, ] signs

I have problem with this "simple" code: 我对此“简单”代码有疑问:

$img_oryg = '<igm class="aligncenter" src="(oryginal img source)" alt="some text | some text">';
$replace_with = '<div class="ivcs_holder"><igm class="aligncenter" src="(oryginal img source)" alt="some text | some text" /></div>';
$content =  preg_replace("#$img_oryg#s", $replace_with , $contetn );
echo $content;

This is ofcourse a sample code, and everything is coded inside a loop, which overlap each image with divs, and also doeas some other stuff. 当然,这是一个示例代码,所有内容都在一个循环内进行编码,该循环将每个图像与div重叠,并且也做一些其他事情。 Problem is in | 问题出在 that can be added to alt-tag of image. 可以添加到图片的alt标签。 If it is present, then code behave strangely and result is not correct. 如果存在,则代码行为异常,结果不正确。

Anybody does know how to deal with strings like that? 有人知道如何处理这样的字符串吗?

it should be ok as long as you run preg_quote on your regex pattern. 只要您在regex模式上运行preg_quote ,就应该可以。 the | | character has a special meaning in regex, so this is why it would cause problems. 字符在正则表达式中具有特殊含义,因此这会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM