Breaking up a string into individual words

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    #1

    Breaking up a string into individual words

    Hi there,

    I have a text box that I will use to search a database. I would like
    to use it so that it will not use a whole string (ie. 'red striped
    top') but instead break it up into individual words (ie. 'red',
    'striped', 'top') and maybe put it into and array. I will then use the
    words in the array to search the database.

    How do I break a string up this way?

    Also, is this the most common way of creating a means of searching a
    database?

    Cheers

    Burnsy
  • Daniel Tryba

    #2
    Re: Breaking up a string into individual words

    mr_burns <bissatch@yahoo .co.uk> wrote:[color=blue]
    > Hi there,
    >
    > I have a text box that I will use to search a database. I would like
    > to use it so that it will not use a whole string (ie. 'red striped
    > top') but instead break it up into individual words (ie. 'red',
    > 'striped', 'top') and maybe put it into and array. I will then use the
    > words in the array to search the database.
    >
    > How do I break a string up this way?[/color]

    Read the Fine Manual, online at

    search for split. But maybe you should look at the PCRE functions...
    [color=blue]
    > Also, is this the most common way of creating a means of searching a
    > database?[/color]

    Don't think so, but depends on what you actually are searching for. "Full
    text searches" might be what you are looking for.

    Comment

    • Janwillem Borleffs

      #3
      Re: Breaking up a string into individual words

      mr_burns wrote:[color=blue]
      > How do I break a string up this way?[/color]

      $array = explode(" ", $string_with_sp aces);
      [color=blue]
      > Also, is this the most common way of creating a means of searching a
      > database?
      >[/color]

      For basic search engines, it probably is. However, you will loose the
      relationship between the words, which is most of the times is implied.

      When you enter "red striped top" in a search engine, you would expect to get
      the matches with all the words at the top.

      This won't happen when you search the database word by word.

      A better way is to use a like clause, which can be build quite easily from a
      string. Here's an example:

      <?php

      $s = "red striped top";
      $q = "select * from table where";

      $array = explode(" ", $s);
      $popped = array();
      $like = "";

      while (count($array) > 1) {
      $clause = implode("%", $array);
      if ($like) $like .= " or";
      $like .= " field like '%$clause%'";
      $popped[] = array_pop($arra y);
      }

      $q .=
      $like .
      ($like ? " or field like '%" : " field like '%") .
      implode(
      "%' or field like '%",
      array_merge($ar ray, $popped)
      ) .
      "%'"
      ;

      print "<code>$q</code>";

      ?>


      JW



      Comment

      • Aditya

        #4
        Re: Breaking up a string into individual words

        Fulltext search is the best way to get your work done if you are using
        MySQL.

        Read these articles on fulltext search:
        http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html


        Best regards,
        Aditya

        Comment

        • Aditya

          #5
          Re: Breaking up a string into individual words

          Fulltext search is the best way to get your work done if you are using
          MySQL.

          Read these articles on fulltext search:
          http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html


          Best regards,
          Aditya

          Comment

          Working...