PHP爆炸和内爆字符串

时间:2015-04-26 21:10:50

标签: php explode implode

我已经完成了一半,并且在输出上发生了爆炸。

我有一个字符串

$text = "test | (3), new | (1), hello | (5)";
$text = explode(",", $text);
foreach ($text as $t){
    $tt = explode(" | ", $t);
    print_r($tt[0]);
}

当我打印上面的数组时,它会根据需要提供test new hello,现在,我需要输入这样的逗号test, new, hello

我搜索并无法实现,因此在此处发布以获得帮助。

3 个答案:

答案 0 :(得分:1)

$text = "test | (3), new | (1), hello | (5)";
echo preg_replace('# \| \(.*?\)#', '', $text);

修改 达到如此结果 '测试',' ' new',' hello'

$text = "test | (3), new | (1), hello | (5)";
$text = preg_replace('# \| \(.*?\)#', '', $text);
echo "'" . preg_replace('#,#', "', '", $text) . "'";

答案 1 :(得分:1)

是的,您可以将它们推送到数组,然后再将implode推送到

$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);

答案 2 :(得分:0)

1- Implode是php中的一个函数,您可以将数组转换为字符串   EX-     

$arr = array('Hello','World!','Beautiful','Day!');

echo implode(" ",$arr);

?>

2- Explode是php中的一个函数,您可以将字符串转换为数组

前 -

$str = "Hello world. It's a beautiful day.";

print_r (explode(" ",$str));

&GT?; `