将大字符串分成较小的块并计算每个块的PHP?

时间:2014-09-11 04:28:09

标签: php mysql codeigniter symfony1

我希望将大字符串分成较小的块并计算数据库中每个块的出现次数

enter image description here

示例:

###Windows###Apple###Android

现在我想在“回答”所有数据库的列中计算Windows,Apple,Android的数量。

答案栏中的字符串可能无法一直修复,这取决于答案!

2 个答案:

答案 0 :(得分:0)

你可以用这种效率很低的方法来做到这一点:

select v.val, count(*)
from (select 'Windows' as val union all
      select 'Apple' union all
      select 'Android'
     ) v join
     table t
     on t.answer like concat('%', v.val, '%')
group by v.val;

答案 1 :(得分:0)

我没有清楚地理解你的问题,但如果我是正确的你想要这样的东西。

首先,您必须查询数据库以获取答案列,然后您必须将答案放在字符串中。然后使用substr_count。就像这样。

<?php
$text = '###Windows###Apple###Android';
echo substr_count($text, 'Windows'); 
?>

希望这就是你所期待的。