5

I need help with regular expressions. What I'm looking for is a regex that looks for link-tags like this:

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

Irrespective of where href="" is positioned, I would like to look it up in the link-tag and put a variable named $url in front of style.css with a / following. If it finds http:// or https:// in front of style.css, then i don't want to put the variable in front of it.

I want every link-tag to be replaced.

4

5 回答 5

3

您可以像这样使用 preg_replace 来归档所需的结果:

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

所以这段代码(假设存储在 $html 和 $url = ' http://mydomain.com/ '):

<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">
于 2009-08-13T18:07:14.397 回答
2

使用正则表达式的解决方案永远不会很漂亮(或可靠),我建议改用 DOM 解析器,并使用其中一种操作方法添加属性。看看 simplehtmldom:

http://simplehtmldom.sourceforge.net/

例如,看看这个:

// 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>
于 2009-08-13T17:15:37.127 回答
2

试试这个正则表达式:

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

替换部分看起来像

\1http://\2\3

或者

$1http://$2$3

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

于 2009-08-13T17:45:10.630 回答
0

I adapted @Juicy Scripter's answer.

It is an improvement for the following.

a) it also works for single quotes as well as double quotes. meaning

/**
 *
 * 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;
    }
于 2013-07-03T06:44:51.670 回答
-2

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

尝试这个:

搜索:href="([^http].*?)"

代替:href="<?php echo $url; ?>/\1"

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

于 2009-08-13T17:13:57.193 回答