1 回答
TA贡献1808条经验 获得超4个赞
对于每个类别,搜索该类别的内容,并将空格替换为内容中不存在的字符。
然后按空格分解内容(这样它将把类别文本保持在一起)。
稍后只需将该字符替换回空格即可。
<?php
$content = "lorem ipsum bla bla bla cat1 bla bla bla kids and baby";
$categories_array = array(
"cat1",
"kids and baby",
"cat3",
);
foreach($categories_array as $category) {
$content = str_replace($category, str_replace(' ', '·', $category), $content);
}
$content_array = explode(' ', $content);
$content_array = array_map(function ($content) {return str_replace('·', ' ', $content);}, $content_array);
var_dump($content_array);
输出:
array(10) {
[0]=>
string(5) "lorem"
[1]=>
string(5) "ipsum"
[2]=>
string(3) "bla"
[3]=>
string(3) "bla"
[4]=>
string(3) "bla"
[5]=>
string(4) "cat1"
[6]=>
string(3) "bla"
[7]=>
string(3) "bla"
[8]=>
string(3) "bla"
[9]=>
string(13) "kids and baby"
}
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报