Php preg_replace将url从相对更改为绝对

Php preg_replace将url从相对更改为绝对,php,regex,preg-replace,preg-match,Php,Regex,Preg Replace,Preg Match,我的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/

我的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 #).
-
-(如果url以#开头,我不想更改)。
请帮帮我

这对你有用

PHP:

函数展开链接($link){
return('href='http://example.com/.trim($link,'\'“/\\').');
}
$textarea=preg\u replace('/href\s*=\s*(?“[^\\”]*“\\'[^\\\']*\”)/e',“扩展链接($1”),$textarea);

我还将正则表达式改为使用双引号或撇号

/(href|src)=['"]([^"']+)['"]/i
替代品保持原样

编辑:

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

修订:

对于第一个正则表达式,我很抱歉,我忘记了第二个用于其中的域的示例

(href|src)=['"](?:http://.+/)?([^"']+)['"]
这应该行得通

那么:

$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";
}

对于一个多行的HTML blob,我不知道如何以贪婪的方式实现这一点。我试过“m”修饰语,但运气不好。你能帮个忙吗?谢谢,正则表达式应该修改一下,使之不受约束,同时也包括https
'~(href | src)=([“\]”](?!#)(?!https?://)/?([^\2]*?)\2~i'
@Jako:你对
https?
是正确的,但是
[^\2]*
不需要取消加密,因为它本身是不加密的。请用一行中的两个URL尝试你的正则表达式:
$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
----------------------------------------------------------------------