php regex获取href标记内的字符串 [英] php regex to get string inside href tag

查看:86
本文介绍了php regex获取href标记内的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式,可以在href标记和引号内为我提供字符串.

I need a regex that will give me the string inside an href tag and inside the quotes also.

例如,我需要在以下位置提取theurltoget.com:

For example i need to extract theurltoget.com in the following:

<a href="theurltoget.com">URL</a>

此外,我只想要基本网址部分. IE.从http://www.mydomain.com/page.html开始,我只想要http://www.mydomain.com/

Additionally, I only want the base url part. I.e. from http://www.mydomain.com/page.html i only want http://www.mydomain.com/

推荐答案

请勿为此使用正则表达式.您可以使用xpath和内置的php函数来获取所需的内容:

Dont use regex for this. You can use xpath and built in php functions to get what you want:

    $xml = simplexml_load_string($myHtml);
    $list = $xml->xpath("//@href");

    $preparedUrls = array();
    foreach($list as $item) {
        $item = parse_url($item);
        $preparedUrls[] = $item['scheme'] . '://' .  $item['host'] . '/';
    }
    print_r($preparedUrls);

这篇关于php regex获取href标记内的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆