I am half way done and struck at imploding on the output.
I have a string as
$text = "test | (3), new | (1), hello | (5)";
$text = explode(",", $text);
foreach ($text as $t){
$tt = explode(" | ", $t);
print_r($tt[0]);
}
When I print the above array, it gives me test new hello
as needed, now, I need to put a comma like this test, new, hello
I searched and could not achieve hence posting here to get help.
$text = "test | (3), new | (1), hello | (5)";
echo preg_replace('# \| \(.*?\)#', '', $text);
EDIT: to reach result like 'test',' 'new', 'hello'
$text = "test | (3), new | (1), hello | (5)";
$text = preg_replace('# \| \(.*?\)#', '', $text);
echo "'" . preg_replace('#,#', "', '", $text) . "'";
Yes, you can push them to array and implode later on
$text = "test | (3), new | (1), hello | (5)";
$text = explode(",", $text);
$arr = array();
foreach ($text as $t){
$tt = explode(" | ", $t);
$arr[] = $tt[0];
}
echo implode(", ", $arr);
1- Implode is a function in php where you can convert a array into string Ex-
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
2- Explode is a function in php where you can convert a string into array
Ex-
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?> `