【问题标题】:php: removing certain character  after detecting string with space/whitespace in the front using regexphp:使用正则表达式检测前面带有空格/空格的字符串后删除某些字符
【发布时间】:2013-04-25 02:17:31
【问题描述】:

我目前正在尝试使用网络爬虫,并遇到了正则表达式的问题。

我想从下面的字符串中存储的字符是“09:00 AM”:

<td style="border: #080707 1px solid;" lang="lang" valign="top" scope="scope"> 09:00 AM</td>

下面是我的正则表达式部分:

preg_match_all ('/<td .+ scope="scope">(.*)<\/td>/i',$link_string,$details);

结果输出是 09:00 AM,我不想要 Â.我知道这是由空格引起的,但我尝试了几种不同的方法,例如:

    preg_match_all ('/<td .+ scope="scope">\s(.*)<\/td>/i',$link_string,$details);

    preg_match_all ('/<td .+ scope="scope">(\w+)<\/td>/i',$link_string,$details);

    preg_match_all ('/<td .+ scope="scope"> (.*)<\/td>/i',$link_string,$details);

但是,返回是假的,我想要的字符不匹配。

希望对执行这种正则表达式的最佳方式有所启发。

【问题讨论】:

  • 看来空格是  所以,我只包含一个额外的元字符来匹配它,如下所示: preg_match_all ('/( )?(\w+.*)/iu',$link_string ,$details);

标签: php html regex whitespace preg-match-all


【解决方案1】:

如果你自己不能trim() td 标签,那么为什么不使用substr() 来将第一个字符去掉:

$time = substr($details[0][1],1) //[0][1] to be changed to actual output

【讨论】:

    【解决方案2】:

    您必须只添加 u 修饰符。使用此标志,正则表达式引擎会将您的字符串视为 unicode 字符串。示例:

    preg_match_all ('/<td .+ scope="scope">(.*)<\/td>/iu',$link_string,$details);
    

    【讨论】:

    • 感谢您的建议,我没有意识到这个标志 /u
    猜你喜欢
    • 2015-10-03
    • 2017-10-01
    • 2018-11-20
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    相关资源
    最近更新 更多