如何将URL转换为另一个使用PHP 为例
domain.com to domain2.com
和
domain.com/content to cdn.domain2.com
答案 0 :(得分:0)
<强> 1。将“domain.com”转换为“domain2.com”
<?php
$re = '/(?=\.)/m';
$str = 'domain.com
';
$subst = '2'; //add text
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
?>
<强> 1。结果强>
domain2.com
<强> 2。将“domain.com/content”转换为“cdn.domain2.com”
<强> REGEXP:强>
的 1 强>
/(?=\.)/m
<强> 2 强>
/\/.*/m
第3 强>
/^(?=\w)/m
<强> 2。代码PHP:
<?php
$re = '/(?=\.)/m';
$str = 'domain.com/content';
$subst = '2';
$result = preg_replace($re, $subst, $str); //domain2.com/content
$re = '/\/.*/m';
$str = $result; //domain2.com
$subst = '';
$result = preg_replace($re, $subst, $str);
$re = '/^(?=\w)/m';
$str = 'domain2.com';
$subst = 'cdn.';
$result = preg_replace($re, $subst, $str); //cdn.domain2.com
echo $result;
?>
<强>结果:强>
cdn.domain2.com
<强>享受强>