<?php
$string = "A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend ";
$pattern = "/A?\.?(\w*)\n+B?\.?(\w*)\n+C?\.?(\w*)\n+D?\.?(\w*)\n+/i";
$replacement = "\$1,\$2,\$3,\$4";
print preg_replace($pattern, $replacement, $string);
?> 试试看,我没有测试,服务挂啦

解决方案 »

  1.   

    这样打印出来还是A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend 能光打印出选项值的吗?不要A. B. C. D. 还能区分A. B. C. D.大小写
      

  2.   

    换成这样呢?$pattern = "/A?\.?(\w*)\s+B?\.?(\w*)\s+C?\.?(\w*)\s+D?\.?(\w*)\s+/i";
      

  3.   

    打印结果 send,her,to,ahospital,get,a,octorin Cbuy,some,medicine,finda friend 
      

  4.   

    能分别打印出 
    send her to a hospital 
    get a doctor in 
    buy some medicine 
    find a friend 
      

  5.   

    $replacement = "$1.'<br>'.$2.'<br>'.$3.'<br>'.$4";
    试一下
      

  6.   

    如果再不行
    $string = "A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend ";
    $pattern = "/A?\.?(\w*)\s+B?\.?(\w*)\s+C?\.?(\w*)\s+D?\.?(\w*)\s+/i";
    preg_match_all($pattern ,$string,$select, PREG_SET_ORDER);
    print_r($select);
    看一下$select数组的值可以调用
      

  7.   

    $s = 'A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend';
    echo preg_replace("/[A-Z]\./","",preg_replace("/.[A-Z]\.|$/","\n",$s));
      

  8.   

    无疑,正则是最好的解决方法,不过我们也可以换点别的方法试试
    <?php
    $string = "A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend  ";
    list($key,$value)=(explode("D.",$string));
    list($key,$value1)=(explode("C.",$key));
    list($key,$value2)=(explode("B.",$key));
    list($key,$value3)=(explode("A.",$key));
    ECHO $value."<br>";
    ECHO $value1."<br>";
    echo $value2."<br>";
    echo $value3."<br>";
    echo "-----------------------"."<br>";
    echo preg_replace("/[A-Z]\./","",preg_replace("/.[A-Z]\.|$/","\n",$string));
    ?> 
      

  9.   

    preg_match('/(?:[Aa]\.)(.*)(?:[Bb]\.)(.*)(?:[Cc]\.)(.*)(?:[Dd]\.)(.*)/','A.send her to a hospital B.get a doctor in C.buy some medicine D.find a friend  ',$match);
    print_r($match); 我综合了一下 写了这个正则表达式 请高手讲解一下 我还没怎么能看懂
      

  10.   

    ?:  :是元字符,在正则表达式模式圆括号内部的前面使用 '?:'来防止存储该匹配供今后使用
    [Cc]\.:就是在匹配C. 
    .     :代表任意字符,而非与时的.所以要在前加转义符\。()是子适配,(.*)就是说在找到A.之后的任意多个字符(.代表任意字符,前已说明,*代表重复0次或是重复多次)
    不知道我说没说清楚。