2

我的 PHP 代码是:

$string = preg_replace('/(href|src)="([^:"]*)(?:")/i','$1="http://mydomain.com/$2"', $string);

它适用于:

 - <a href="aaa/">Link 1</a> => <a href="http://mydomain.com/aaa/">Link 1</a>
 - <a href="http://mydomain.com/bbb/">Link 1</a> => <a href="http://mydomain.com/bbb/">Link 1</a>

但不是:

- <a href='aaa/'>Link 1</a>
- <a href="#top">Link 1</a> (I don't want to change if url start by #).

请帮我!

4

3 回答 3

2

怎么样:

$arr = array('<a href="aaa/">Link 1</a>',
             '<a href="http://mydomain.com/bbb/">Link 1</a>',
             "<a href='aaa/'>Link 1</a>",
             '<a href="#top">Link 1</a>');
foreach( $arr as $lnk) {
    $lnk = preg_replace('~(href|src)=(["\'])(?!#)(?!http://)([^\2]*)\2~i','$1="http://mydomain.com/$3"', $lnk);
    echo $lnk,"\n";
}

输出:

<a href="http://mydomain.com/aaa/">Link 1</a>
<a href="http://mydomain.com/bbb/">Link 1</a>
<a href="http://mydomain.com/aaa/">Link 1</a>
<a href="#top">Link 1</a>

解释:

The regular expression:

(?-imsx:(href|src)=(["\'])(?!#)(?!http://)([^\2]*)\2)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    href                     'href'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    src                      'src'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  =                        '='
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    ["\']                    any character of: '"', '\''
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    #                        '#'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    http://                  'http://'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    [^\2]*                   any character except: '\2' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  \2                       what was matched by capture \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-10-15T07:51:22.490 回答
0

This will work for you

PHP:

function expand_links($link) {
    return('href="http://example.com/'.trim($link, '\'"/\\').'"');
}

$textarea = preg_replace('/href\s*=\s*(?<href>"[^\\"]*"|\'[^\\\']*\')/e', 'expand_links("$1")', $textarea);

I also changed the regex to work with either double quotes or apostrophes

于 2013-10-15T03:58:47.867 回答
0

试试这个作为你的模式

/(href|src)=['"]([^"']+)['"]/i

替换保持原样

编辑:

等等……我没有测试前两种链接类型,只是那些不起作用的……给我一点时间

修订:

对第一个正则表达式感到抱歉,我忘记了与其中的域一起使用的第二个示例

(href|src)=['"](?:http://.+/)?([^"']+)['"]

那应该工作

于 2013-10-15T05:22:04.313 回答