<?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);
?> 试试看,我没有测试,服务挂啦
send her to a hospital
get a doctor in
buy some medicine
find a friend
试一下
$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数组的值可以调用
echo preg_replace("/[A-Z]\./","",preg_replace("/.[A-Z]\.|$/","\n",$s));
<?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));
?>
print_r($match); 我综合了一下 写了这个正则表达式 请高手讲解一下 我还没怎么能看懂
[Cc]\.:就是在匹配C.
. :代表任意字符,而非与时的.所以要在前加转义符\。()是子适配,(.*)就是说在找到A.之后的任意多个字符(.代表任意字符,前已说明,*代表重复0次或是重复多次)
不知道我说没说清楚。