【问题标题】:php regex : get src valuephp 正则表达式:获取 src 值
【发布时间】:2009-03-30 04:20:06
【问题描述】:

如何在 php 中使用正则表达式检索所有 src 值?

<script type="text/javascript" src="http://localhost/assets/javascript/system.js" charset="UTF-8"></script>
<script type='text/javascript' src='http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c' charset='UTF-8'></script>

检索到的值应该只包含:

谢谢。

【问题讨论】:

    标签: php regex


    【解决方案1】:
    /src=(["'])(.*?)\1/
    

    示例:

    <?php
    
    $input_string = '<script type="text/javascript" src="http://localhost/assets/javascript/system.js" charset="UTF-8"></script>';
    $count = preg_match('/src=(["\'])(.*?)\1/', $input_string, $match);
    if ($count === FALSE) 
        echo('not found\n');
    else 
        echo($match[2] . "\n");
    
    $input_string = "<script type='text/javascript' src='http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c' charset='UTF-8'></script>";
    $count = preg_match('/src=(["\'])(.*?)\1/', $input_string, $match);
    if ($count === FALSE) 
        echo('not found\n');
    else 
        echo($match[2] . "\n");
    

    给予:

    http://localhost/assets/javascript/system.js
    http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c
    

    【讨论】:

    • 嗨,斯科特,谢谢。但是我使用的是 preg_match_all 而你的似乎没有输出任何东西。我正在使用 preg_match_all("/http:\/\/(.*?)[^\"']+/", $scripts, $matches, PREG_SET_ORDER); 现在
    • preg_match 总是返回一个整数值。
    【解决方案2】:

    也许只有我一个人,但我不喜欢使用正则表达式在 HTML 片段中查找内容,尤其是当 HTML 不可预测时(可能来自用户或其他网页)。

    这样的事情怎么样:

    $doc =
    <<<DOC
        <script type="text/javascript" src="http://localhost/assets/javascript/system.js" charset="UTF-8"></script>
        <script type='text/javascript' src='http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c' charset='UTF-8'></script>
    
    DOC;
    
    $dom = new DomDocument;
    $dom->loadHTML( $doc );
    
    $elems = $dom->getElementsByTagName('*');
    
    foreach ( $elems as $elm ) {
        if ( $elm->hasAttribute('src') )
            $srcs[] = $elm->getAttribute('src');
    }
    
    print_r( $srcs );
    

    我不知道这个和正则表达式之间的速度差异是什么,但我花了更少的时间来阅读它并理解我想要做什么。

    【讨论】:

    • 感谢尼克的替代方案,但我会坚持使用正则表达式,因为它最多有几行 javascripts 文件。也许这是一个偏好问题。 :)
    【解决方案3】:

    我同意 Nick 的观点,使用 DomDocument 对象来获取您的数据。这是一个 xpath 版本:

    $doc =
    <<<DOC
        <script type="text/javascript" src="http://localhost/assets/javascript/system.js" charset="UTF-8"></script>
        <script type='text/javascript' src='http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c' charset='UTF-8'></script>
    DOC;
    
    $doc = new DomDocument;
    $doc->loadHTML($doc);
    
    $xpath = new DomXpath($doc);
    $elements = $xpath->query('//[@src]');
    
    foreach($elements as $element)
    {
        echo $element->nodeValue;
    }
    

    【讨论】:

    • 谢谢Alex,请参考上面的评论。
    【解决方案4】:

    如果你决定走正则表达式路线,这应该对你有用

    /(?<=\<).*?src=(['"])(.*?)\1.*?(?=/?\>)/si
    

    【讨论】:

      【解决方案5】:

      jQuery 方法

      var Scripts = [];
      $('head script').each(function(){
          if($(this).attr('type') == 'text/javascript' && $(this).attr('src')){
              Scripts.push($(this).attr('src'));
          }
      });
      console.log(Scripts)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        相关资源
        最近更新 更多