tags:

views:

115

answers:

3
Hello! 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î„­â„¢. Good job!

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

+9  A: 

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;
Sam152
This is hot. I like how neat it is and efficient it is.
Doug
+1  A: 

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);
Andrei Serdeliuc
+3  A: 

You could try some regular expressions:

$matches = array();
preg_match("/Hello! You are with (.*?)\. Good job!/",$input,$matches);
$name = $matches[1];
Austin Hyde
+1 for using regexps. the only proper tool for the job
stereofrog
@stereofrog It's slower than `str_replace`.
chelmertz