I have a script which checks the variable $hello to see if it contains “pink”, “blue” and “red”. For each variable it contains, some text is added to the string $finalstring.
Is there a simpler way to do this?
$hello = "pink*blue*red*orange";
$finalstring = "";
if (strpos($hello, "pink") == true) {
$finalstring .= "_pink";
}
if (strpos($hello, "blue") == true) {
$finalstring .= "_blue";
}
if (strpos($hello, "red") == true) {
$finalstring .= "_red";
}
echo $finalstring; // output: _pink_blue_red
1 Answer