5

正規表現のサポートが必要です。私が探しているのは、次のようなリンクタグを探す正規表現です。

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

href = ""の位置に関係なく、link-tagで検索し、$urlという名前の変数をstyle.cssの前に/を付けて配置します。style.cssの前にhttp://またはhttps://が見つかった場合、その前に変数を配置したくありません。

すべてのリンクタグを置き換えてほしい。

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 パーサーを使用し、その操作メソッドの 1 つを使用して属性を追加することをお勧めします。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 に答える
0

@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;
    }
于 2013-07-03T06:44:51.670 に答える
-2

単一のファイルを編集していると思います-テキストエディターまたはIDEは、正規表現の検索/置換を実行できるはずです。

これを試して:

探す:href="([^http].*?)"

交換:href="<?php echo $url; ?>/\1"

これを PHP で使用する必要がある場合は、preg_replace を使用してください。検索文字列の前後にスラッシュが必要であることを覚えておいてください。

于 2009-08-13T17:13:57.193 に答える