如何删除部分句子?

时间:2009-11-08 08:23:50

标签: php

你和安迪在一起。干得好!

Hello! You are with Naruto-san. Good job!
Hello! You are with DragonFangîŠ. Good job!
Hello! You are with Adam Chan. Good job!
Hello! You are with Dudeî„­. Good job!
Hello! You are with Signore. Good job!
Hello! You are with Athena. Good job!
Hello! You are with EL NwithJA. Good job!
Hello! You are with Keeperî„­â„¢. Good job!

使用PHP,我怎样才能删除“你好!你是谁”和“。干得好!”?所以我可以将名称存储到变量中。

3 个答案:

答案 0 :(得分:9)

试试这个:

$line="Hello! You are with Andy. Good job!";
$bad=array("Hello! You are with ",". Good job!");
$name = str_replace($bad,"",$line);
echo $name;

答案 1 :(得分:4)

您可以尝试一些正则表达式:

$matches = array();
preg_match("/Hello! You are with (.*?)\. Good job!/",$input,$matches);
$name = $matches[1];

答案 2 :(得分:1)

因为看起来你的句子中唯一改变的是名字,你可以这样使用str_ireplace

$sentence = "Hello! You are with Andy. Good job!";
$name = str_ireplace(array('Hello! You are with ', '. Good job!'), array('',''), $sentence);