已定义的str_word_count的打印列表在分解的数组中按索引匹配

时间:2011-05-05 14:24:33

标签: php arrays count

我之前从@HSZ获得了this matter的一些帮助,但是在使用现有阵列的解决方案时遇到了麻烦。我试图做的是爆炸引号,使所有单词大写加上每个单词索引值,只回显我定义然后内爆。简单来说,总是删除引号中的第4个单词或第3个和第4个单词......这也可以用正则表达式完成。

示例:

Hello [1] => World [2] => This [3] => Is [4] => a [5] => Test ) 6仅输出我定义的数字,例如 1 - (Hello) [2] - (World) [3],[4],[5]和[6]或这是一个测试只留下 Hello World ,或1和[6]你好测试...

如:

echo $data[1] + ' ' + $data[6]; //Would output index 1 and 6 Hello and Test

现有代码

if (stripos($data, 'test') !== false) {
$arr = explode('"', $data);

for ($i = 1; $i < count($arr); $i += 2) {
$arr[$i] = strtoupper($arr[$i]);    
$arr[$i] = str_word_count($arr, 1); // Doesnt work with array of course.
}

$arr = $matches[1] + ' ' + $matches[6];
$data = implode('"', $arr);
}

1 个答案:

答案 0 :(得分:1)

假设$data'Hello world "this is a test"'$arr = explode('"', $data)将与以下内容相同:

$arr = array (
  [0] => 'Hello World',
  [1] => 'This is a test'
)

如果您想使用this is a test执行某些操作,可以使用$testarr = explode(' ', $arr[1]);等内容将其展开。

然后您可以执行以下操作:

$matches = array();
foreach ($testarr as $key => $value) {
   $value = strtoupper($value);
   if(($key+1)%4 == 0) { // % is modulus; the result is the remainder the division of two numbers. If it's 0, the key+1 (compensate for 0 based keys) is divisible by 4.
      $matches[] = $value;
   }
}
$matches = implode('"',$matches);