[英]Replace a particular word in a sentence with another word in php
if($_GET["op"]=="replace"){
$str=$_GET["input"];
$pos=$_GET["input2"];
$apos=explode(',',$pos);
echo"After replaced:".str_replace($apos[0],$apos[1],$str);
}
当我给一个输入句子hi this is a good day
,并给予字被替换,因为is
。 让另一个单词替换为not
。
然后输出将是: hi thnot not good day
。
hi this not good day
怎么能让输出hi this not good day
我想你想要这样的东西:
preg_replace("/\b(is)\b/", "not", $input_lines);
if($_GET["op"]=="replace"){
$str=$_GET["input"];
$pos=$_GET["input2"];
$apos=explode(',',$pos);
echo"After replaced:".preg_replace("/\b(".$apos[0].")\b/", $apos[1], $str)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.