This script works fine by getting the values of each first word in a sentence Eg.(Ann and Mike). My issue is that the output of an array is not what I want.
I was expecting $result_names = array("Ann","Mike");
but I got
Array ( [0] => Ann [1] => Ann Mike )
Here is the Code
$names = array();
$str = 'Ann is good,"Mike loves cooking"';
$result = explode(',',$str); //parsing with , as delimiter
$op = "";
foreach($result as $value)
{
$tmp = explode(' ',$value);
$op .= trim($tmp[0],"'"")." ";
$names[] = $op;
}
$op = rtrim($op);
$rr = ucwords($op);
print_r($names);
2
Answers
Your code concatenates your values:
Try to replace this by just an assignment
Documentation