php爆炸值并创建空格

时间:2015-06-04 14:41:51

标签: php explode

我在数据库中有这样的字符串。很抱歉,但我可以更改数据库。

TuesLunch|MonLunch|MonDinner|TuesBKfast

所以一般来说它的所有日子都有晚餐,午餐和BKfast 现在我想让这个字符串在php中显示如下

Tues Lunch 
Mon Lunch 
Mon Dinner
Tues BKfast

到目前为止我已经完成了

$weekdays = TuesLunch|MonLunch|MonDinner|TuesBKfast;
$days = explode('|',$weekdays);

有人能告诉我怎么做吗?任何帮助和建议都会非常明显。感谢

3 个答案:

答案 0 :(得分:0)

您可以使用基本为preg_split的{​​{1}}和正则表达式:(正则表达式拼写为大写字母)

explode()

答案 1 :(得分:0)

您可以使用正则表达式执行此操作:

$weekdays = "TuesLunch|MonLunch|MonDinner|TuesBKfast";
$days = explode('|', $weekdays);

$strings = [];
foreach ($days as $day) {
  preg_match_all('/[A-Z][^A-Z]*/', $day, $results);
  $strings[] = implode($results[0], ' ');
}
print_r($strings);

答案 2 :(得分:0)

这将按预期工作:

$weekdays = "TuesLunch|MonLunch|MonDinner|TuesBKfast";
$days = explode('|', $weekdays);

function spacify($str, $delim = ' ') {
    $str = preg_replace('/([a-z0-9])([A-Z])/', "$1$delim$2", $str);
    return $str;
}

foreach (spacify($days) as $key => $val){
    echo $val."<br>";
}