要查找到 sql 数据库 php 中的数组字符串


Array string to find into sql database, php

我尝试在sql数据库中查找字符串。如果我有字符串"a b c d e",为了获得准确性结果,我尝试"选择...从。。。其中列 = 'a b c d e' 或列='a b c d' 或 column='a b c' 或..." ,我试图找到像这样获取内爆数组的方法:

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

诸如此类?

谢谢。

您可以使用 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
)

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

  $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
)
<?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 />';
   }
?>

在此处查看结果。