2009-11-08 38 views
1

你和安迪在一起。做得好!如何刪除句子的某些部分?

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,我該如何去除「你好!你和」以及「幹得好!」?所以我可以將名稱存儲到變量中。

回答

9

試試這個:

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

這很熱。我喜歡它是多麼的整潔和高效。 – Strawberry

1

,因爲它看起來就像是發生在你的句子中的唯一的事情就是名字,你可以使用str_ireplace這樣的:

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

你可以嘗試一些正則表達式:

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

@stereofrog它比'str_replace'慢。 – chelmertz

相關問題