忽略换行符的正则表达式

时间:2014-03-21 16:07:35

标签: php regex

我不太擅长正则表达式。

我有各种文件,里面有重复的字符串:

$find = "><script contentType=\"application/x-javascript\"\n>\n\nif(event.target.hostContainer)";

但有时候你可以在上面的字符串中看到2 \n而不是2 \n,有时候会有3或1.但是,这是一个必须克服的愚蠢问题,但不幸的是文件是pdf。 ..所以我不能控制它的输出。

如何在忽略$file = file_get_contents('pdfs/another1.pdf'); $find = "><script contentType=\"application/x-javascript\"\n>\n\nif(event.target.hostContainer)"; $replace = "whatever bla bla"; $output_str = str_replace($find, $replace, $file); 的情况下搜索上述字符串。

我的问题的背景是:

{{1}}

1 个答案:

答案 0 :(得分:1)

首先,str_replace不对搜索字符串使用正则表达式。正确的函数是preg_replace

这是一个适用于这种情况的正则表达式:

$find = '#><script contentType="application/x-javascript"\s*>\s*if\(event\.target\.hostContainer\)#U';
$output_str = preg_replace($find, $replace, $file);

正则表达式有很多“\”(转义)字符,因为“。”,“(”和“)”在正则表达式中有特殊含义。正则表达式包含在“#”分隔符中。正则表达式末尾的“U”修饰符是一种预防措施,因此如果字符串具有多个匹配表达式,则每个匹配都将替换为替换。

有关PHP正则表达式的完整说明,请访问:http://us1.php.net/manual/en/reference.pcre.pattern.syntax.php