1

I try the way to find string in sql database. I thing if i have string "a b c d e", for accuracy result, i try "select...from...where column = 'a b c d e' or column='a b c d' or column='a b c' or..." , i try to find the way to get array for implode like this:

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

like that ?

Thank you very much.

7
  • --What have you tried? Commented Sep 11, 2013 at 2:25
  • For search on sql database, i explode string before and implode after but how can i get array like that to implode it to sql query. Commented Sep 11, 2013 at 2:29
  • I think you'd have to construct the array by yourself. iterate how much array you'd want then implode the strings accordingly. Commented Sep 11, 2013 at 2:32
  • i don't know how much array to search result accuracy, i try all array like this (a b c d) or (a b c) or ... Commented Sep 11, 2013 at 2:39
  • why? are you sure you don't just need an array of the words? Your array seems kinda redundant to me... [Hello,everybody,nice,to,meet,you] Commented Sep 11, 2013 at 2:56

3 Answers 3

1

You can tokenize the string with preg_split(), build the array and then reverse it with 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));

Output:

Array
(
    [0] => foo bar baz
    [1] => foo bar
    [2] => foo
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I find the way.
1

I'm not sure why you'd do this, but this will do it:

  $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);

Here is the output:

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
)

Comments

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

?>

See the result here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.