Okay,
So here is my code:
$explode = explode("+ ", $article);
$explode_count = count($explode);
for($i=0;$i<$explode_count;$i++)
{
$numbers = preg_replace('/[^0-9]/', '', $explode[$i]);
$letters = preg_replace('/[^a-zA-Z]/', ' ', $explode[$i]);
if($letters == "All Star Game")
{
echo "Done";
}
}
The $letters variable is equal to All Star Game. But, for some reason, “Done” isn’t being echoed. Could it be something with the preg_replace function that I’m using to separate the numbers from the letters in the string? I notice that when I change the $letters variable to
$letters = preg_replace('/[^a-zA-Z]/', '', $explode[$i]);
and set the rest of the code to:
if($letters == "AllStarGame")
{
echo "Done";
}
Then, php echoes it out. What’s going on in regards to spaces here?
If you have an input with
+ 1933 All Star Game +then your regex will convert the text snippet into something like␣␣␣␣␣␣All␣Star␣Game␣. (The␣represents a space).That’s because you
preg_replace('/[^a-zA-Z]/', ' ')any non-letter with a space there. So the1933will become four spaces, the two surrounding spaces will remain. Which is why your final comparison of$letters == "All Star Game"will never match.