开发者

How do I remove parts of a sentence?

You are with Andy. Good job!

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î„­â„¢.开发者_StackOverflow Good job!

Using PHP, how can I strip "Hello! You are with " and ". Good job!"? so I can store the name into a variable.


Try this:

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


You could try some regular expressions:

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


Since it looks like the only thing that changes in your sentences is the name, you could use str_ireplace like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜