I have a php string that outputs like this:
San Jose, California
I need the php string to output like this:
SanJose, Caliornia
I've been playing around with str_replace but not having much luck.
Any help would be appreciated!
I have a php string that outputs like this:
San Jose, California
I need the php string to output like this:
SanJose, Caliornia
I've been playing around with str_replace but not having much luck.
Any help would be appreciated!
Update: first explode with
$string = explode(",",$string);
then do for all value of $string ( in foreach for example )
For just spaces, use str_replace:
$string = str_replace(' ', '', $string);
For all whitespace, use preg_replace:
$string = preg_replace('/\s+/', '', $string);
look at here How to strip all spaces out of a string in php?
Maybe it is not very nice, but you can explode
and then concatenate:
$your_string="San Jose, California";
$arr = explode(" ", $your_string);
$result = $arr[0] . $arr[1] . " " . $arr[2];
You can try substr_replace
$str = "San Jose, California" ;
echo substr_replace($str, '', strpos($str, " "), 1);
Output
SanJose, California
Given that the string is a CSV, you could explode it, process each value, and then implode it.
$string = "San Jose, California";
$values = explode(",", $string);
foreach($values as &$value)
{
$value = str_replace(" ", "", $value);
}
$output = implode(", ", $values);
*untested
Don't use regulars expressions, it is overkill (slower). Use the count parameter of strreplace:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
From PHP manual:
count
If passed, this will be set to the number of replacements performed.
$str = "San Jose, California";
$result = str_replace(" ", "", $str, 1);