Home  >  Article  >  Backend Development  >  php 用正则处理 字符窜的截取解决办法

php 用正则处理 字符窜的截取解决办法

WBOY
WBOYOriginal
2016-06-13 13:15:24791browse

php 用正则处理 字符窜的截取
比如有下面一个例子:

$string="{dede:pagestyle maxwidth='600' pagepicnum='12' ddmaxwidth='200' row='3' col='4' value='2'/}
{dede:img ddimg=' /uploads/allimg/120804/488-120P40U014.jpg ' text='鸟瞰图' width='709' height='749'} /uploads/allimg/120804/488-120P40U014.jpg {/dede:img}
{dede:img ddimg='/uploads/allimg/120804/488-120P40U213.jpg' text='酒店鸟瞰图' width='567' height='399'} /uploads/allimg/120804/488-120P40U213.jpg {/dede:img}
{dede:img ddimg='/uploads/allimg/120804/488-120P40U228.jpg' text='商业沿街效果图' width='1476' height='629'} /uploads/allimg/120804/488-120P40U228.jpg {/dede:img}
{dede:img ddimg='/uploads/allimg/120804/488-120P40U238.jpg' text='效果图' width='567' height='450'} /uploads/allimg/120804/488-120P40U238.jpg {/dede:img}
{dede:img ddimg='/uploads/allimg/120804/488-120P40U243.jpg' text='沿街效果图' width='567' height='354'} /uploads/allimg/120804/488-120P40U243.jpg {/dede:img}";

通过处理循环输出:ddimg,text两个属性

输出结果: /uploads/allimg/120804/488-120P40U014.jpg 鸟瞰图

  /uploads/allimg/120804/488-120P40U213.jpg 酒店鸟瞰图

  /uploads/allimg/120804/488-120P40U228.jpg 商业沿街效果图

  /uploads/allimg/120804/488-120P40U238.jpg 效果图
  
  /uploads/allimg/120804/488-120P40U243.jpg' 沿街效果图

同时计算出图片的张数。。

大家帮帮忙 谢谢

------解决方案--------------------

PHP code
preg_replace("/ddimg='([^\']*)'\s+text='([^\']*)'/e",'$arr["$1"]="$2"',$string);
print_r($arr);
<br><font color="#e78608">------解决方案--------------------</font><br>
$ar=array_slice($arr,0,4);<br>print_r($ar);<br>echo count($ar); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn