Search Result help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sharif T. Karim

    Search Result help

    I am trying to do the following with my search script that looks for records
    in a mysql table. The following is an example of what I am trying to do.

    Text being searched:
    --
    The brown fox jumped over the green fence then jumped into the web monitor.
    It was hurt so it jumped backwards and fell on its!
    --

    The word we're searching for "web".

    The results should look like

    --
    ...then jumped into the *web* monitor. It was...
    --

    Are there any functions out there that will do this? Or what would it take
    to make one?

    --
    Sharif T. Karim
    ....you don't know wrath yet...


  • Michael Austin

    #2
    Re: Search Result help

    Sharif T. Karim wrote:
    [color=blue]
    > I am trying to do the following with my search script that looks for records
    > in a mysql table. The following is an example of what I am trying to do.
    >
    > Text being searched:[/color]

    The brown fox jumped over the green fence then jumped into the web
    monitor. It was hurt so it jumped backwards and fell on its!


    Return all records and use something like:

    database query:

    select field from table where field like "% web %";
    // in this case the space before will get you web and not webster etc...


    $pattern = "web";
    $string = [string returned from mysql]
    $replacement = "<b>*$patte rn*</b>"
    echo preg_replace($p attern, $replacement, $string);

    Michael Austin.
    Consultant - Available.
    Donations welcomed. Http://www.firstdbasource.com/donations.html
    :)

    Comment

    • Sharif T. Karim

      #3
      Re: Search Result help

      Michael Austin, being the foo Michael Austin is, wrote:[color=blue]
      > Sharif T. Karim wrote:
      >[color=green]
      >> I am trying to do the following with my search script that looks for
      >> records in a mysql table. The following is an example of what I am
      >> trying to do.
      >>
      >> Text being searched:[/color]
      >
      > The brown fox jumped over the green fence then jumped into the web
      > monitor. It was hurt so it jumped backwards and fell on its!
      >
      >
      > Return all records and use something like:
      >
      > database query:
      >
      > select field from table where field like "% web %";
      > // in this case the space before will get you web and not webster
      > etc...
      >
      >
      > $pattern = "web";
      > $string = [string returned from mysql]
      > $replacement = "<b>*$patte rn*</b>"
      > echo preg_replace($p attern, $replacement, $string);
      >[/color]

      Thanks, but that only replaces words. I am trying to get the result
      ($string) in this case, to start showing the part where the first occurence
      of $pattern is.

      --
      Sharif T. Karim
      ....you don't know wrath yet...


      Comment

      • Tim Van Wassenhove

        #4
        Re: Search Result help

        In article <gOyIc.71373$a9 2.1047@twister. nyc.rr.com>, Sharif T. Karim wrote:[color=blue]
        > I am trying to do the following with my search script that looks for records
        > in a mysql table. The following is an example of what I am trying to do.
        >
        > Text being searched:
        > --
        > The brown fox jumped over the green fence then jumped into the web monitor.
        > It was hurt so it jumped backwards and fell on its!
        > --
        >
        > The word we're searching for "web".[/color]

        I'm pretty sure you'll find an answer to this in a newsgroup about mysql
        or in the mysql manual.

        In case mysql doesn't have such a function, you could always use the '%
        $var %' query and then use strpos en substr to get the desired result.


        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw>

        Comment

        • Pedro Graca

          #5
          Re: Search Result help

          Sharif T. Karim wrote:[color=blue]
          > Are there any functions out there that will do this? Or what would it take
          > to make one?[/color]

          str_replace(); preg_replace()
          strpos()
          substr()
          strlen()


          Check them out at the fine manual

          http://www.php.net/<function>

          eg
          Replace all occurrences of the search string with the replacement string


          --
          USENET would be a better place if everybody read: | to email me: use |
          http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
          http://www.netmeister.org/news/learn2quote2.html | header, textonly |
          http://www.expita.com/nomime.html | no attachments. |

          Comment

          • Michael Austin

            #6
            Re: Search Result help

            Sharif T. Karim wrote:[color=blue]
            > Michael Austin, being the foo Michael Austin is, wrote:
            >[color=green]
            >>Sharif T. Karim wrote:
            >>
            >>[color=darkred]
            >>>I am trying to do the following with my search script that looks for
            >>>records in a mysql table. The following is an example of what I am
            >>>trying to do.
            >>>
            >>>Text being searched:[/color]
            >>
            >>The brown fox jumped over the green fence then jumped into the web
            >>monitor. It was hurt so it jumped backwards and fell on its!
            >>
            >>
            >>Return all records and use something like:
            >>
            >>database query:
            >>
            >>select field from table where field like "% web %";
            >>// in this case the space before will get you web and not webster
            >>etc...
            >>
            >>
            >>$pattern = "web";
            >>$string = [string returned from mysql]
            >>$replacemen t = "<b>*$patte rn*</b>"
            >>echo preg_replace($p attern, $replacement, $string);
            >>[/color]
            >
            >
            > Thanks, but that only replaces words. I am trying to get the result
            > ($string) in this case, to start showing the part where the first occurence
            > of $pattern is.
            >[/color]

            I can't always give you the exact syntax. what I gave you was close, but
            you need to be able to take what you are given and test/modify it to
            suit your needs...

            $search-from-form = $_POST['inputfield'];
            $pattern = "-$search-from-form-";
            //sear pattern needs non-alphanumberic delimiters
            $string = "The web is a scary place to be";
            //string to be searched
            $rpat = "$search-from-form";
            //now replace the string with the search "word"
            $replacement = "<b>*$rpat* </b>";
            echo "STRING = $string<br>\n";
            echo preg_replace($p attern, $replacement, $string);

            results in
            The *web* is a scary place to be
            (only in your browser *web* is bold).

            This took a whole 10 minutes to prepare... is this for your job or just
            playing/learning? Did you happen to read the docs on the previous
            example? If not, why not???


            Michael Austin.
            Consultant - Available.
            Donations welcomed. Http://www.firstdbasource.com/donations.html
            :)

            Comment

            • Michael Austin

              #7
              Re: Search Result help

              Tim Van Wassenhove wrote:
              [color=blue]
              > In article <gOyIc.71373$a9 2.1047@twister. nyc.rr.com>, Sharif T. Karim wrote:
              >[color=green]
              >>I am trying to do the following with my search script that looks for records
              >>in a mysql table. The following is an example of what I am trying to do.
              >>
              >>Text being searched:
              >>--
              >>The brown fox jumped over the green fence then jumped into the web monitor.
              >>It was hurt so it jumped backwards and fell on its!
              >>--
              >>
              >>The word we're searching for "web".[/color]
              >
              >
              > I'm pretty sure you'll find an answer to this in a newsgroup about mysql
              > or in the mysql manual.
              >
              > In case mysql doesn't have such a function, you could always use the '%
              > $var %' query and then use strpos en substr to get the desired result.
              >
              >[/color]
              unfortunately it looks like another "programmer " that shouldn't be as
              he/she was already given the answer, he/she just couldn't figure out how
              to use it... No it wasn't perfect, but you should at least read the docs
              - and if you can't find them, the above statement really does apply...
              And companies actually hire people like this??


              Michael Austin.
              Consultant - Available.
              Donations welcomed. Http://www.firstdbasource.com/donations.html
              :)

              Comment

              • Chung Leong

                #8
                Re: Search Result help

                "Sharif T. Karim" <sharif@nyc.rr. com> wrote in message
                news:gOyIc.7137 3$a92.1047@twis ter.nyc.rr.com. ..[color=blue]
                > I am trying to do the following with my search script that looks for[/color]
                records[color=blue]
                > in a mysql table. The following is an example of what I am trying to do.
                >
                > Text being searched:
                > --
                > The brown fox jumped over the green fence then jumped into the web[/color]
                monitor.[color=blue]
                > It was hurt so it jumped backwards and fell on its!
                > --
                >
                > The word we're searching for "web".
                >
                > The results should look like
                >
                > --
                > ..then jumped into the *web* monitor. It was...
                > --
                >
                > Are there any functions out there that will do this? Or what would it take
                > to make one?
                >
                > --
                > Sharif T. Karim
                > ...you don't know wrath yet...[/color]

                Hey, think I got the winning entry!

                <?

                $text = 'The brown fox jumped over the green fence then jumped into
                the web monitor. It was hurt so it jumped backwards and fell on its!';

                $word = 'web';

                $pattern = '/\b(.{0,24})\b(' .preg_quote($wo rd).')\b(.{0,16 }\S)\b/si';

                if(preg_match($ pattern, $text, $matches)) {
                extract($matche s, EXTR_PREFIX_ALL , 'm');
                echo "..$m_1*$m_2*$m _3...";
                }

                ?>

                Do I get a prize?


                --
                Obey the Clown - http://www.conradish.net/bobo/


                Comment

                • Sharif T. Karim

                  #9
                  Re: Search Result help

                  Michael Austin, being the foo Michael Austin is, wrote:[color=blue]
                  > Sharif T. Karim wrote:[color=green]
                  >> Michael Austin, being the foo Michael Austin is, wrote:
                  >>[color=darkred]
                  >>> Sharif T. Karim wrote:
                  >>>
                  >>>
                  >>>> I am trying to do the following with my search script that looks
                  >>>> for records in a mysql table. The following is an example of what
                  >>>> I am trying to do.
                  >>>>
                  >>>> Text being searched:
                  >>>
                  >>> The brown fox jumped over the green fence then jumped into the web
                  >>> monitor. It was hurt so it jumped backwards and fell on its!
                  >>>
                  >>>
                  >>> Return all records and use something like:
                  >>>
                  >>> database query:
                  >>>
                  >>> select field from table where field like "% web %";
                  >>> // in this case the space before will get you web and not webster
                  >>> etc...
                  >>>
                  >>>
                  >>> $pattern = "web";
                  >>> $string = [string returned from mysql]
                  >>> $replacement = "<b>*$patte rn*</b>"
                  >>> echo preg_replace($p attern, $replacement, $string);
                  >>>[/color]
                  >>
                  >>
                  >> Thanks, but that only replaces words. I am trying to get the result
                  >> ($string) in this case, to start showing the part where the first
                  >> occurence of $pattern is.
                  >>[/color]
                  >
                  > I can't always give you the exact syntax. what I gave you was close,
                  > but you need to be able to take what you are given and test/modify
                  > it to suit your needs...
                  >
                  > $search-from-form = $_POST['inputfield'];
                  > $pattern = "-$search-from-form-";
                  > //sear pattern needs non-alphanumberic delimiters
                  > $string = "The web is a scary place to be";
                  > //string to be searched
                  > $rpat = "$search-from-form";
                  > //now replace the string with the search "word"
                  > $replacement = "<b>*$rpat* </b>";
                  > echo "STRING = $string<br>\n";
                  > echo preg_replace($p attern, $replacement, $string);
                  >
                  > results in
                  > The *web* is a scary place to be
                  > (only in your browser *web* is bold).
                  >
                  > This took a whole 10 minutes to prepare... is this for your job or
                  > just playing/learning? Did you happen to read the docs on the previous
                  > example? If not, why not???
                  >
                  >
                  > Michael Austin.
                  > Consultant - Available.
                  > Donations welcomed. Http://www.firstdbasource.com/donations.html
                  > :)[/color]

                  You obviously are such a genius that you completely missed the point to the
                  question. Part of it was to highlight keywords, that part I know how to do.
                  The other part, the one you keep looking past, is how to display only the
                  part of the result where the first occurrence of the keyword is.

                  Say the text var being searched:

                  $text = 'PHP - The best programming language on the face of the planet. The
                  manual is at php dot net. It is ... awesome!';

                  The keywords in the search
                  $words = 'manual';

                  I'd like the result to display as:

                  ....The *manual* is at php dot...

                  Do you get me now? Look at your code, it does NOT do that. It simply bolds
                  the keywords.

                  Take it easy guy...
                  --
                  Sharif T. Karim
                  ....you don't know wrath yet...


                  Comment

                  Working...