Php HTML中链接标记的正则表达式

Php HTML中链接标记的正则表达式,php,regex,html-parsing,link-tag,Php,Regex,Html Parsing,Link Tag,我需要正则表达式方面的帮助。我要找的是一个正则表达式,它可以查找如下链接标记: <link rel="stylesheet" href="style.css" type="text/css"> 无论href=”“位于何处,我都希望在链接标记中查找它,并在style.css前面放置一个名为$url的变量,并带有/following。如果它在style.css前面找到http://或https://则我不想将变量放在它前面 我想替换每个链接标记。我猜您正在编辑一个文件-您的文本编辑

我需要正则表达式方面的帮助。我要找的是一个正则表达式,它可以查找如下链接标记:

<link rel="stylesheet" href="style.css" type="text/css">

无论href=”“位于何处,我都希望在链接标记中查找它,并在style.css前面放置一个名为$url的变量,并带有/following。如果它在style.css前面找到http://或https://则我不想将变量放在它前面


我想替换每个链接标记。

我猜您正在编辑一个文件-您的文本编辑器或IDE应该能够为您执行正则表达式搜索/替换

试试这个:

搜索:
href=“([^http].*?”

替换:
href=“/\1”


如果您需要在PHP中使用它,请使用preg_replace。请记住,搜索字符串前后都需要一个正斜杠。

使用正则表达式解决这个问题永远不会很好(或可靠),我建议改用DOM解析器,并使用其操作方法之一添加属性。看看simplehtmldom:

例如,请看以下内容:

// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

$html->find('div', 1)->class = 'bar';

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
//从字符串创建DOM
$html=str_get_html('HelloWorld');
$html->find('div',1)->class='bar';
$html->find('div[id=hello]',0)->innertext='foo';
echo$html;//产出:fooWorld

试试这个正则表达式:

/(<link.*href=["'])(style.css)(["'].[^>]*>)/gi 


注意:您可能需要根据对字符串的引用方式对其中一个引号进行转义。

您可以像这样使用preg\u replace来存档所需的结果:

preg_replace('/(<link\b.+href=")(?!http)([^"]*)(".*>)/', '$1'.$url.'$2$3$4', $html);

preg_replace(”/(I改编了@Juicy Scripter的答案

这是对以下方面的改进

a) 它也适用于单引号和双引号
/**
 *
 * Take in html content as string and find all the <script src="yada.js" ... >
 * and add $prepend to the src values except when there is http: or https:
 *
 * @param $html String The html content
 * @param $prepend String The prepend we expect in front of all the href in css tags
 * @return String The new $html content after find and replace. 
 * 
 */
    protected static function _prependAttrForTags($html, $prepend, $tag) {
        if ($tag == 'css') {
            $element = 'link';
            $attr = 'href';
        }
        else if ($tag == 'js') {
            $element = 'script';
            $attr = 'src';
        }
        else if ($tag == 'img') {
            $element = 'img';
            $attr = 'src';
        }
        else {
            // wrong tag so return unchanged
            return $html;
        }
        // this checks for all the "yada.*"
        $html = preg_replace('/(<'.$element.'\b.+'.$attr.'=")(?!http)([^"]*)(".*>)/', '$1'.$prepend.'$2$3$4', $html);
        // this checks for all the 'yada.*'
        $html = preg_replace('/(<'.$element.'\b.+'.$attr.'='."'".')(?!http)([^"]*)('."'".'.*>)/', '$1'.$prepend.'$2$3$4', $html);
        return $html;
    }
/**
*
*以字符串形式接收html内容并查找所有
*并将$prepend添加到src值中,除非存在http:或https:
*
*@param$html字符串html内容
*@param$prepend String我们希望在css标记中的所有href前面使用的前缀
*@return String查找并替换后的新$html内容。
* 
*/
受保护的静态函数_prependAttrForTags($html,$prepend,$tag){
如果($tag=='css'){
$element='link';
$attr='href';
}
如果($tag=='js',则为else){
$element='script';
$attr='src';
}
else if($tag=='img'){
$element='img';
$attr='src';
}
否则{
//标签错误,所以返回不变
返回$html;
}
//这将检查所有的“yada.*”
$html=preg_replace(“/()/”,“$1.”$prepend.$2$3$4',$html);
返回$html;
}

这也会影响超链接,例如,这不是一个好主意。在文本编辑器或IDE中,您可以在选择中替换,而在PHP中,您通常可以将头与体分开进行解析。使用DOM parser进行此操作有点过火(IMO)罕见的文档是有效的(并且需要额外的处理),并且DOM解析比regexp占用的内存要多得多。这是一个非常好的答案。但是当link元素使用单引号时,它失败了。我自己已经扩展了这个答案。请参见此处。看起来我无法在某些情况下将其适配为img元素。请注意,此函数不适用于img在某些情况下的元素。将在我得到答案后更新此内容。谢谢。对我来说效果很好。
preg_replace('/(<link\b.+href=")(?!http)([^"]*)(".*>)/', '$1'.$url.'$2$3$4', $html);
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="style2.css" type="text/css">
<link rel="stylesheet" href="http://google.com/style3.css" type="text/css">
<link rel="stylesheet" href="style4.css" type="text/css">
<link rel="stylesheet" href="https://google.com/style5.css" type="text/css">
<link rel="stylesheet" href="some/path/to/style6.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/style.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/style2.css" type="text/css">
<link rel="stylesheet" href="http://google.com/style3.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/style4.css" type="text/css">
<link rel="stylesheet" href="https://google.com/style5.css" type="text/css">
<link rel="stylesheet" href="http://mydomain.com/some/path/to/style6.css" type="text/css">
/**
 *
 * Take in html content as string and find all the <script src="yada.js" ... >
 * and add $prepend to the src values except when there is http: or https:
 *
 * @param $html String The html content
 * @param $prepend String The prepend we expect in front of all the href in css tags
 * @return String The new $html content after find and replace. 
 * 
 */
    protected static function _prependAttrForTags($html, $prepend, $tag) {
        if ($tag == 'css') {
            $element = 'link';
            $attr = 'href';
        }
        else if ($tag == 'js') {
            $element = 'script';
            $attr = 'src';
        }
        else if ($tag == 'img') {
            $element = 'img';
            $attr = 'src';
        }
        else {
            // wrong tag so return unchanged
            return $html;
        }
        // this checks for all the "yada.*"
        $html = preg_replace('/(<'.$element.'\b.+'.$attr.'=")(?!http)([^"]*)(".*>)/', '$1'.$prepend.'$2$3$4', $html);
        // this checks for all the 'yada.*'
        $html = preg_replace('/(<'.$element.'\b.+'.$attr.'='."'".')(?!http)([^"]*)('."'".'.*>)/', '$1'.$prepend.'$2$3$4', $html);
        return $html;
    }