[if hrefs]
这样的代码,里面的hrefs是不固定的,如:[if xxx],用正则替换成<?php if(xxx){ ?>,
麻烦哪位神人告知下,谢了.在线等.

解决方案 »

  1.   

    $s = '[if hrefs]';echo preg_replace('/\[if (.+)]/', '<?php if($1){ ?>', $s);
    <?php if(hrefs){ ?>
    如果你是在做模板引擎,请参考成熟的项目。不要自己瞎闯
      

  2.   

    嘿嘿,大哥,多谢你的提醒,我只是在做一个类似于碎片形式的组件, 把一些代码用特定的语法转换成php模板.但是我不懂正则,所以就来找个人指导下.
    我问题说的不是很清楚, $s的值不是只有那么点, 它是一大串的代码段,只把其中[if hrefs]给替换成php的if判断,你的这段代码替换了之后后面的"]"替换不掉额.比如这样吧,
    <div class="clearfix">
    [loop]
    [if hrefs]<a href="[url]" target="[target]" title="[alt]">[/if]<img width="[width]" height="[height]" alt="" src="[src]" alt="[alt]" complete="complete"/>[if hrefs]</a>[/if]
    [/loop]
    </div>
    这样一段代码,把其中的[if hrefs]换成php判断,麻烦再告诉一下,谢了.
      

  3.   

    $s = <<< TXT
    <div class="clearfix">
    [loop]
    [if hrefs]<a href="[url]" target="[target]" title="[alt]">[/if]<img width="[width]" height="[height]" alt="" src="[src]" alt="[alt]" complete="complete"/>[if hrefs]</a>[/if]
    [/loop]
    </div>
    TXT;
    echo preg_replace('/\[if (.+?)\]/', '<?php if($1){ ?>', $s);