把这段test1<[email protected]>;test2 <[email protected]>;test3<[email protected]>;替换成
"test1" <[email protected]>;"test2" <[email protected]>;"test3" <[email protected]>;
请注意其中的空格,谢谢大家!

解决方案 »

  1.   

    $s = "test1<[email protected]>;test2 <[email protected]>;test3<[email protected]>;";echo preg_replace('/(test\d+)\s*&/', '"$1" &', $s);
      

  2.   

    $s = 'test1<[email protected]>;test2 <[email protected]>;test3<[email protected]>;';$s = preg_replace('/(test\d*)(<| <)/', '"$1" <', $s);echo $s;
      

  3.   

    这个还要补充下,并不一定是这个test*之类的 或者其他名称 或 汉字 要都可以匹配
      

  4.   

    echo preg_replace('/(\w+)\s*&lt/', '"$1" &lt', $s);  
      

  5.   

    姓名6<[email protected]>;姓名11<[email protected]>;姓名3<[email protected]>
    怎么匹配替换成那个格式
      

  6.   

    <?php
    $str = <<<CODE
    test1&lt;[email protected]&gt;;test2 &lt;[email protected]&gt;;test3&lt;[email protected]&gt;;
    CODE;$pattern = '/(.*?)\s*&lt;\[email protected]&gt;;\s*/is';
    $replacement = '"\1" &lt;\[email protected]&gt;;';
    $result = preg_replace($pattern,$replacement , $str);echo $result;
    ?>
      

  7.   

    echo preg_replace('/([^;]+)\s*&lt/U', '"$1" &lt', $s); 
      

  8.   

    不好意思,我的错,还有个需求,就是姓名中间会有空格
    比如
    Mr Chen&lt;[email protected]&gt;;姓名 PHP&lt;[email protected]&gt;;姓 测 试&lt;[email protected]&gt;
      

  9.   

    "Mr Chen" <[email protected]>;"姓名 PHP" <[email protected]>;"姓 测 试" <[email protected]>没有问题啊