且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php regex:获取src值

更新时间:2022-05-12 22:18:05

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

Maybe it is just me, but I don't like using regular expressions for finding things in pieces of HTML, especially when the HTML is unpredictable (perhaps comes from a user or other web pages).

怎么样呢?

$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 );

我不知道此表达式与正则表达式之间的速度差是多少,但是花了我少得多的时间来阅读它并了解我要执行的操作.

I don't know what the speed difference is between this and a regular expression but it takes me a heck of a lot less time to read it and understand what I'm trying to do.