1

我尝试在 sql 数据库中查找字符串的方法。如果我有字符串“abcd e”,为了准确的结果,我会尝试“select...from...where column = 'abcd e' or column='abc d' or column='ab c' or.. ." ,我尝试找到像这样获取内爆数组的方法:

[0]=>a b c d e,
[1]=>a b c d,
[2]=>a b c,
...
[n]=>a
Can i do 

像那样 ?

非常感谢。

4

3 回答 3

1

我不确定你为什么要这样做,但这会做到:

  $string = "Hello everybody, nice to meet you";
  $parts = explode(" ", $string);
  $new_array = Array();

  for($i = 0; $i < count($parts); $i++)
  {
      $new_array[$i] = '';
      for($j = 0; $j < count($parts) - $i; $j++)
      {
          $new_array[$i] = $new_array[$i]." ".$parts[$j];
      }
  }

  print_r($new_array);

这是输出:

Array
(
    [0] =>  Hello everybody, nice to meet you
    [1] =>  Hello everybody, nice to meet
    [2] =>  Hello everybody, nice to
    [3] =>  Hello everybody, nice
    [4] =>  Hello everybody,
    [5] =>  Hello
)
于 2013-09-11T02:51:48.507 回答
1

您可以用 标记字符串preg_split(),构建数组,然后用 反转它array_reverse()

$string = 'foo bar baz';
$results = array(); $current = '';
foreach (preg_split('/ +/', $string) as $token) {
    $current = strlen($current) ? "$current $token" : $token;
    $results[] = $current;
}

print_r(array_reverse($results));

输出:

Array
(
    [0] => foo bar baz
    [1] => foo bar
    [2] => foo
)
于 2013-09-11T02:56:25.483 回答
1
<?php

   $string = "Hello everybody, nice to meet you";

   //remove the comma
   $string = str_replace(',', '', $string);

   //explode for each words with ' '(space) delimiter
   $path = explode(' ', $string);
   $final = array();

   array_push($final, implode(' ', $path));

   foreach($path as $el){
       //remove max array element
       $last_el = count($path) - 1;
       unset($path[$last_el]);

       //set the final array one by one.
       array_push($final, implode(' ', $path));
   }

   //Show the result.
   foreach($final as $row){
       echo $row . ' <br />';
   }

?>

在这里查看结果

于 2013-09-11T03:00:06.300 回答