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

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“正则提取的url中的域名以及替换域名的方法 preg_match()和preg_replace() <syntaxhighlight lang="php"> <?php //网站的url $url = 'http://ww…”
 
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的4个中间版本)
第1行: 第1行:
== 取网址 ==
方法一:
<syntaxhighlight lang="javascript">
/(http|https):\/\/([\w.]+\/?)\S*/</syntaxhighlight> 
方法二:
<syntaxhighlight lang="javascript">
/http[s]{0,1}:\/\/([\w.]+\/?)\S*/</syntaxhighlight>
使用 js 提取域名(带协议)
<syntaxhighlight lang="javascript">
url.replace(/(http[s]{0,1}:\/\/[a-zA-Z0-9.-]+)?.*/i,'$1')</syntaxhighlight>
方法三:
{{Double-columns|<syntaxhighlight lang="javascript">
const str = 'http://s3.qhmsg.com/qs/;mapsapi;m/1001.js';
const reg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;
const result= str.replace(reg,"kangkk.com");
console.log(result);
</syntaxhighlight>|{{consoleout|<pre>'http://kangkk.com/qs/;mapsapi;m/1001.js'
</pre>}}}}
== PHP ==
正则提取的url中的域名以及替换域名的方法 preg_match()和preg_replace()
正则提取的url中的域名以及替换域名的方法 preg_match()和preg_replace()


<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';
   
   
第29行: 第56行:
     $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>

2026年11月26日 (四) 05:05的最新版本

取网址

方法一:

/(http|https):\/\/([\w.]+\/?)\S*/

 

方法二:

/http[s]{0,1}:\/\/([\w.]+\/?)\S*/

使用 js 提取域名(带协议)

url.replace(/(http[s]{0,1}:\/\/[a-zA-Z0-9.-]+)?.*/i,'$1')

方法三:

const str = 'http://s3.qhmsg.com/qs/;mapsapi;m/1001.js';
const reg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/; 
const result= str.replace(reg,"kangkk.com");
console.log(result);
'http://kangkk.com/qs/;mapsapi;m/1001.js'

PHP

正则提取的url中的域名以及替换域名的方法 preg_match()和preg_replace()

<?php 
    $url = 'http://www.baidu.com/index.php'; //网站的url
    $reg = '/(http):\/\/([^\/]+)/i'; //正则表达式
    preg_match($reg, $url,$res);
?>
$res的结果
    array (size=3)
=> string 'http://www.baidu.com' (length=20)
=> string 'http' (length=4)
=> string 'www.baidu.com' (length=13)
<?php 
    $url1 = 'www.jingdong.com';
 
    /**
    Example #1 使用后向引用紧跟数值原文
    */
    echo preg_replace($reg, 'http://'.$url1, $url);
 
        
    /**
    Example #2 preg_replace()中使用基于索引的数组
    */
    $patterns[0] = '/'.$res[2].'/';
    $replacements[0] = $url1;
    echo preg_replace($patterns, $replacements, $url);
 ?>
http://www.jingdong.com/index.php