I know that to replace spaces I can use:
str_replace(' ', ';', $string);
My question is...how do I replace only the first space?
eg: firstword secondword thirdword
to firstword;secondword thirdword
I know that to replace spaces I can use:
str_replace(' ', ';', $string);
My question is...how do I replace only the first space?
eg: firstword secondword thirdword
to firstword;secondword thirdword
I would use preg_replace
$subject='firstword secondword thirdword';
$result = preg_replace('%^([^ ]+?)( )(.*)$%', '\1;\3', $subject);
var_dump($result);
You could use regular expressions with preg_replace, or use stripos. So you would do this:
<?php
$original = 'firstword secondword thirdword';
$result =
substr($original,0,stripos($original,' '))
.';'
.substr($original,stripos($original,' ')+1);
echo $result;
See it running here.