‘壹’ 如何用php正则替换图片的域名地址
preg_replace('/src="A.com/','src="B.com',$body);
‘贰’ PHP的preg_replace 正则替换
preg_replace("/http:\/\//","",$message)
$message = preg_replace(array(
"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
"/http:\/\//" //加的,,
), $allowimgcode ? array(
"bbcodeurl('\\1', '<img src=\"%s\" onload=\"thumbImg(this)\" alt=\"\" />')",
"bbcodeurl('\\3', '<img width=\"\\1\" height=\"\\2\" src=\"%s\" border=\"0\" alt=\"\" />')"
) : array(
"bbcodeurl('\\1', '<a href=\"%s\" target=\"_blank\">%s</a>')",
"bbcodeurl('\\3', '<a href=\"%s\" target=\"_blank\">%s</a>')"
), $message);
其实你也可以在它处理完后加preg_replace("/http:\/\//","",$message)这句.
‘叁’ php如何进行正则替换
按照你的要求把h后的数字和w后的任意数字替换成固定数的php程序如下
<?php
$fix='555';//固定数
$str='asdasda/w/100/h/200/q/sdasdsad';
$regex1="~h/[0-9]+~";
$result=preg_replace($regex1,"h/".$fix,$str);
$regex2="~w/[0-9]+~";
$result=preg_replace($regex2,"w/".$fix,$result);
print_r($result);
?>
运行结果
asdasda/w/555/h/555/q/sdasdsad
‘肆’ PHP正则表达式如何匹配出域名
看看我下面的例子代码:
<?php
$s='http://www.abc.com
http://www.def.com/
https://www.ghl.com/';
if (preg_match_all('#https?://(.*?)($|/)#m', $s, $r)) print_r($r[1]);
?>
执行的结果是:
E:\ygb>php a.php
Array
(
[0] => www.abc.com
[1] => www.def.com
[2] => www.ghl.com
)