Regular/替换域名:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
无编辑摘要
Kangkk​(留言 | 贡献)
无编辑摘要
第4行: 第4行:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
<?php  
<?php  
    //网站的url
     $url = 'http://www.baidu.com/index.php'; //网站的url
     $url = 'http://www.baidu.com/index.php';
     $reg = '/(http):\/\/([^\/]+)/i'; //正则表达式
    //正则表达式
     $reg = '/(http):\/\/([^\/]+)/i';
     preg_match($reg, $url,$res);
     preg_match($reg, $url,$res);
    /**  $res的结果
?></syntaxhighlight>
            array (size=3)
<blockquote><pre>
$res的结果
    array (size=3)
=> string 'http://www.baidu.com' (length=20)
=> string 'http://www.baidu.com' (length=20)
=> string 'http' (length=4)
=> string 'http' (length=4)
=> string 'www.baidu.com' (length=13)
=> string 'www.baidu.com' (length=13)
    */
</pre></blockquote>
    //要替换的位置替换成什么
 
<syntaxhighlight lang="php">
<?php
     $url1 = 'www.jingdong.com';
     $url1 = 'www.jingdong.com';
   
   
第30行: 第32行:
     $replacements[0] = $url1;
     $replacements[0] = $url1;
     echo preg_replace($patterns, $replacements, $url);
     echo preg_replace($patterns, $replacements, $url);
    //结果为 http://www.jingdong.com/index.php
  ?></syntaxhighlight>
  ?></syntaxhighlight>
<blockquote><pre>
http://www.jingdong.com/index.php
</pre></blockquote>