doushui5587 2014-03-21 16:07
浏览 229

忽略换行符的正则表达式

I am not very good at regular expressions.

I have various files that have a repeated string inside them:

$find = "><script contentType=\"application/x-javascript\"
>

if(event.target.hostContainer)";

But sometimes instead of the 2 you can see in the above string, there is sometimes 3 or 1. Granted, it's a stupid problem to have to overcome but unfortuantely the file is a pdf... soo i don't have control over its output.

How might i go about searching for the above string while ignoring the .

The context of my question is:

$file = file_get_contents('pdfs/another1.pdf');
$find = "><script contentType=\"application/x-javascript\"
>

if(event.target.hostContainer)";

$replace = "whatever bla bla";

$output_str = str_replace($find, $replace, $file);
  • 写回答

1条回答 默认 最新

  • duanlie1298 2014-03-21 17:23
    关注

    For one thing, str_replace doesn't use regular expressions for the search string. The correct function is preg_replace.

    Here's a regex that works in this case:

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

    The regex has a lot of "\" (escape) characters because ".", "(", and ")" have special meaning in regex. The regex is enclosed in the '#' delimiter. The 'U' modifier at the end of the regex is a precaution so that if the string has more than one matching expression, each match gets replaced with the replacement.

    A complete explanation of PHP regex is available here: http://us1.php.net/manual/en/reference.pcre.pattern.syntax.php

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧