我的GetBetween函数需要获得多个结果

时间:2016-09-02 07:55:09

标签: php

我一直在使用此功能获取标签之间的内容,因为它比 preg_match_all 更快(几乎100次查询一次)。

function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}

$content = ",john,,benny,,steven,gerard,";
$usercount = substr_count($content,",") / 2;

for ($t=0;$t<$usercount;$t++)
{  

$users = GetBetween($content,",",",");
echo $users

}

然而,它只给了我一个结果。我应该使用哪种方法来获得所有结果?

1 个答案:

答案 0 :(得分:1)

要获得逗号(作为分隔符)之间的所有单词,请使用以下简单方法explodearray_filter函数:

$content = ",john,,benny,,steven,gerard,";
$words = array_filter(explode(",", $content));
// now you can easily iterate through $words array outputting each word
print_r($words);

输出:

Array
(
    [1] => john
    [3] => benny
    [5] => steven
    [6] => gerard
)