【问题标题】:regex with php pattern [closed]带有php模式的正则表达式[关闭]
【发布时间】:2015-01-17 06:56:46
【问题描述】:
<h3 class="r"><a href="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CCUQFjAA&amp;url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F18682352%2Fgoogle-search-results-with-php&amp;ei=9ptsVNivF_iTsQTv-YJ4&amp;usg=AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ&amp;bvm=bv.80120444,d.cWc" onmousedown="return rwt(this,'','','','1','AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ','','0CCUQFjAA','','',event)" data-href="http://stackoverflow.com/questions/18682352/google-search-results-with-php">curl - Google search results with php - Stack Overflow</a></h3>

data-href 值正则表达式模式。

【问题讨论】:

  • 请阅读How to ask questions on StackOverflow 所以不是让人们为您创建代码的地方。向我们展示您的尝试。
  • 你的问题听起来很模糊,你能说得更具体一些吗?为什么?

标签: php regex


【解决方案1】:

可以试试这个模式

$str = '<h3 class="r"><a href="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CCUQFjAA&amp;url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F18682352%2Fgoogle-search-results-with-php&amp;ei=9ptsVNivF_iTsQTv-YJ4&amp;usg=AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ&amp;bvm=bv.80120444,d.cWc" onmousedown="return rwt(this,\'\',\'\',\'\',\'1\',\'AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ\',\'\',\'0CCUQFjAA\',\'\',\'\',event)" data-href="http://stackoverflow.com/questions/18682352/google-search-results-with-php">curl - Google search results with php - Stack Overflow</a></h3>';
$re = '/data-href=["\']?([^"\' ]*)["\' ]/is';
preg_match($re, $str, $m);
$attr_value = trim(str_replace('data-href=', '', $m[0]), '"');
echo $attr_value;

输出:

http://stackoverflow.com/questions/18682352/google-search-results-with-php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多