我需要能够搜索字符串并替换诸如“ me”,“ my”,“ i”之类的单词,并将其替换为诸如“ you”和“ your”之类的单词。基本上,我需要能够更改主题的拥有权。
User input: "Can you remind me to feed my dog tonight?"
Response : "Sure I can remind you to: feed your dog tonight"
其中“提醒我”是一个命令,因此可以忽略,但应将“我的”更改为您的命令。但是,我将把字符“ m”和“ y”的每个集合更改为“ your”。所以像astronomy
现在这样的话astronoyour
。我不要
public static function userToServer($string){
$pos = array(' me '=>'you', ' i '=>'you','we'=>'we','us'=>'you','you'=>' i ', ' my '=>'your');
foreach($pos as $key => $value){
if(strpos($string, $key) !== false){
$result = str_replace ($key, $value, $string );
print_r($result);
return $result;
}
}
return null;
}
public static function parse($string, $commands){
foreach($commands as $c){
if(stripos($string,$c) !== false){
$params = explode($c, $string);
$com[$c] =$params[1];
}
}
if(isset($params)){
foreach($com as $key => $value){
if($key ==='remind me to'){
//$user->addEventToCalender($value);
//echo $value;
echo 'Okay, I will remind you to '.String::userToServer($value);
}
}
}
}
以及使用上述功能的示例输出
Using $_GET['input'] as $input
Original: Will you remind me to secure funds for my business
Response: Okay, I will remind you to secure funds for my byouiness
我确定我必须创建自己的方式来解决此问题,但是我想问一下,因为我知道regex是一个很好的工具,而且我可能会忽略解决方案。
此演示网址显示了我正在使用的当前代码,并将在收到答案时进行更新。请随时查看:)
直接链接到有问题的字符串类:http : //api.austinkregel.com/butler/string-class
<?php
function userToServer($string){
$in=array('#\bcat\b#','#\bdog\b#');
$out=array('fish','frog');
$string=preg_replace($in,$out,$string);
return $string;
}
echo userToServer("cat eat dog but not doge"); //fish eat frog but not doge
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句