PHP正则表达式IMG标签转换问题,谢谢!! ereg('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',$str,$regs)$str="[localimg=".$regs[2].",".$regs[4].",".$regs[6]."]"试试 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 偶用SimpleXML做,可以忽略很多不必要的复杂性$str = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachimg_235058" width="321" height="199" alt="" />';$xml = new SimpleXMLElement($str);preg_match("/\d+$/", $xml[aid], $aid);$out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";echo $out."\n"; $str = preg_replace('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=".$2.",".$4.",".$6."]" ,$str) $str = preg_replace('( <img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=$2,$4,$6]" ,$str)这个试试 $str = preg_replace('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=$2,$4,$6]" ,$str) 这个试试 非常感谢2位老兄的帮忙,特别是myvicy老兄!!实际上我就是想用preg_replace转换的,但是利用了myvicy老兄的代码,程序执行报错,还请帮忙!!Warning: preg_replace() [function.preg-replace]: Unknown modifier '( $str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/','[localimg=$1,$2,$3]',$str); 还有发的代码有可能csdn在正则中自动加空格,如果发现有请删除一下. 利用下面代码搞定!!$str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/','[localimg=$1,$2,$3]',$str);不过现在遇到新的问题了<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" /><img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" /><img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" /><img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100004" alt="" />如何实现不含有width或height的img标签转为[localimg=235058,0,0] [localimg=235058,321,0] [localimg=235058,0,199] 谢谢!!! <?php//下面的$str是你要分析的签$str = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" />';$xml = new SimpleXMLElement($str);preg_match("/\d+$/", $xml[aid], $aid);//进行判断,没有设0if($xml[width]=="")$xml[width]=0;if($xml[height]=="")$xml[height]=0;$out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";echo $out."\n";?> <?php $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />'; $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />'; // 回调函数 function formatimg($matches) { if($2=="")$2=0 if($3=="")$3=0 return "[localimg=".$1.",".$2.",",$3."]" } echo preg_replace_callback( "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/", "formatimg", $text);?> <?php $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />'; $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />'; // 回调函数 function formatimg($matches) { if($2=="")$2=0 if($3=="")$3=0 return "[localimg=".$1.",".$2.",".$3."]" } echo preg_replace_callback( "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/", "formatimg", $text);?> 这要执行二次替换才行$str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s*(?:width="?([^"]+)"?)?\s*(?:height="?([^"]+)"?)?[^>]+>/','[localimg=$1,$2,$3]',$str);$str=preg_replace('/((?<=,)(?=,)|(?<=,)(?=\]))/','0',$str); <?php $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />'; $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />'; // 回调函数 function formatimg($matches) { if($matches[2]=="")$matches[2]=0 if($matches[3]=="")$matches[3]=0 return "[localimg=".$matches[1].",".$matches[2].",".$matches[3]."]" } echo preg_replace_callback( "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/", "formatimg", $text);?> 非常感谢myvicy,我继续学习,现在先结帖!!! $text = ' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';//$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';//$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" alt="" />';if(preg_match_all('/<img.*?aid="?[^"]*_(\d+)"?\s+?(?:width="?([^"]+)"?)?(?:\s+)?(?:height="?([^"]+)"?)?[^>]+>/',$text,$s)){ $c = count($s); $r = count($s[0]); foreach(range(1,$r) as $num){ $mm =array(); for ($i = 1;$i< $c;$i++) { $mm[] = $s[$i][$num-1]|0; } $result[] = '[localimg='.join(",",$mm).']'; } print_r($result);} 不好意思,已经结帖了,明天再来给你加分!!!/Aylazhang 首先谢谢各位老兄的大力帮助,不过现在遇到了新的问题,还是请各位老兄继续能帮忙!!我发现我的img代码并不是很规则,现在遇到的情况就是:<img height="199" src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" width="321" border="0" aid="attachment_100001" />类似与这样的标签如何转换,谢谢!! .htaccess到httpd.ini伪静态规则的转换 wamp安装后绑定到火狐浏览器,全是空白。高手请教啊 php使用GD库加文字水印如何实现换行 求一个递归程序~~~~~~~~~~~~~~~~~ apache问题 求一个匹配注释的正则表达式 php图片验证码校验问题,两个文件间使用session传值,有实例! 怎样用foreach 展开二维数组 phpmyadmin中的config.inc.php需要进行哪些基本配置才能使用?那些旧版本的配置说明都没法用了。 php5.5怎么连接SQLsever 怎么配置 调查一下国内有多少人在用ethna. 【求助】PHP获取input矩阵的方法?
$xml = new SimpleXMLElement($str);preg_match("/\d+$/", $xml[aid], $aid);$out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";
echo $out."\n";
这个试试
这个试试
实际上我就是想用preg_replace转换的,但是利用了myvicy老兄的代码,程序执行报错,还请帮忙!!
Warning: preg_replace() [function.preg-replace]: Unknown modifier '(
$str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/','[localimg=$1,$2,$3]',$str);
不过现在遇到新的问题了
<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />
<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />
<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" />
<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100004" alt="" />如何实现不含有width或height的img标签转为
[localimg=235058,0,0]
[localimg=235058,321,0]
[localimg=235058,0,199]
谢谢!!!
//下面的$str是你要分析的签
$str = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" />';
$xml = new SimpleXMLElement($str);
preg_match("/\d+$/", $xml[aid], $aid);
//进行判断,没有设0
if($xml[width]=="")$xml[width]=0;
if($xml[height]=="")$xml[height]=0;
$out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";
echo $out."\n";
?>
$text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
$text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
// 回调函数
function formatimg($matches) {
if($2=="")$2=0
if($3=="")$3=0
return "[localimg=".$1.",".$2.",",$3."]"
} echo preg_replace_callback(
"/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
"formatimg",
$text);
?>
$text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
$text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
// 回调函数
function formatimg($matches) {
if($2=="")$2=0
if($3=="")$3=0
return "[localimg=".$1.",".$2.",".$3."]"
} echo preg_replace_callback(
"/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
"formatimg",
$text);
?>
$str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s*(?:width="?([^"]+)"?)?\s*(?:height="?([^"]+)"?)?[^>]+>/','[localimg=$1,$2,$3]',$str);
$str=preg_replace('/((?<=,)(?=,)|(?<=,)(?=\]))/','0',$str);
$text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
$text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
// 回调函数
function formatimg($matches) {
if($matches[2]=="")$matches[2]=0
if($matches[3]=="")$matches[3]=0
return "[localimg=".$matches[1].",".$matches[2].",".$matches[3]."]"
} echo preg_replace_callback(
"/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
"formatimg",
$text);
?>
d="attachment_100001" width="321" height="199" alt="" />';
//$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" a
id="attachment_100002" width="321" alt="" />';
//$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" a
id="attachment_100003" alt="" />';
if(preg_match_all('/<img.*?aid="?[^"]*_(\d+)"?\s+?(?:width="?([^"]+)"?)?(?:\s+)?(?:height="?([^"]+)"?)?[^>]+>/',$text,$s)){
$c = count($s);
$r = count($s[0]);
foreach(range(1,$r) as $num){
$mm =array();
for ($i = 1;$i< $c;$i++) {
$mm[] = $s[$i][$num-1]|0;
}
$result[] = '[localimg='.join(",",$mm).']';
}
print_r($result);
}
/Aylazhang
<img height="199" src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" width="321" border="0" aid="attachment_100001" />
类似与这样的标签如何转换,谢谢!!