douduan5753 2010-10-20 08:56
浏览 46
已采纳

“未知”修饰符“c”在正则表达式中的含义是什么? [重复]

This question already has an answer here:

I'm a newbie with regular expressions and i need some help :).

I have this:

$url = '<img src="http://mi.url.com/iconos/oks/milan.gif" alt="Milan">';
$pattern = '/<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i';

preg_match_all($pattern, $url, $matches);

print_r($matches);

And I get this error:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'c'

I want to select that 'milan.gif'.

How can I do that?

</div>
  • 写回答

2条回答 默认 最新

  • dongzaobei0942 2010-10-20 09:06
    关注

    If you’re using / as delimiter, you need to escape every occurrence of that character inside the regular expression. You didn’t:

    /<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i
                                  ^
    

    Here the marked / is treated as end delimiter of the regular expression and everything after is is treated as modifier. i is a valid modifier but c isn’t (see your error message).

    So:

    /<img src="http:\/\/mi\.url\.com\/iconos\/oks\/(.*)" alt="(.*)"\>/i
    

    But as Pekka already noted in the comments, you shouldn’t try to use regular expressions on a non-regular language like HTML. Use an HTML parser instead. Take a look at Best methods to parse HTML.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大