eregi or similar_text < -- problem either way

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

    #1

    eregi or similar_text < -- problem either way

    (driving me nuts)
    Hi there. I wonder if anyone can help?

    I'm including a page from Google in search.php, passing some
    parameters. So far so good. Then I'm asking to look through that
    Google page for a text match, and return true or false.
    eregi returns false whatever the case,
    similar_text returns true whatever the case.

    Can someone sort me out - oops, HELP me out ?
    Thanks thanks in advance !

    (code)
    $page="http://www.google.com/search$search";
    // check to see if on page 1
    $text="my_text_ here";
    if (eregi('/\b$text/i', '$page')) {
    echo "Match";
    } else {
    echo "No Match";
    }
    (/code)

    OR

    (code)
    $page="http://www.google.com/search$search";
    // check to see if on page 1
    $text="my_text_ here";
    if (similar_text('/\b$text/i', '$page')) {
    echo "Match";
    } else {
    echo "No Match";
    }
    (/code)
  • Pedro Graca

    #2
    Re: eregi or similar_text &lt; -- problem either way

    george wrote:[color=blue]
    > $page="http://www.google.com/search$search";
    > // check to see if on page 1
    > $text="my_text_ here";
    > if (eregi('/\b$text/i', '$page')) {
    > echo "Match";
    > } else {
    > echo "No Match";
    > }[/color]

    Never matches.

    You are checking for
    a slash followed by a back slash, b, dollar sign, t, e, x, t, slash, i
    in
    dollar sign, p, a, g, e


    If you want to check for
    backslash, b, m, y, underscore, t, e, x, t, underscore, h, e, r, e
    in
    h, t, t, p, colon, slash, slash, ...

    the right eregi() is
    eregi('\b' . $text, $page)


    The slashes and last "i" you put in the eregi are used for preg_*
    functions (which are better than their ereg* counterparts).

    [color=blue]
    > $page="http://www.google.com/search$search";
    > // check to see if on page 1
    > $text="my_text_ here";
    > if (similar_text('/\b$text/i', '$page')) {
    > echo "Match";
    > } else {
    > echo "No Match";
    > }[/color]

    In my tests I only managed to have similar_text() return 0 (false) with
    similar_text('' , $something) or similar_text($s omething, '')
    Any two non-empty strings will return at least 1 (true).

    The strings you are comparing are here are
    slash, backslash, b, dollar sign, t, ...
    and
    dollar sign, p, a, g, e

    To compare
    m, y, underscore, t, e, x, t, underscore, ...
    and
    h, t, p, p, colon, slash, slash, ...

    the right similar_text() is
    similar_text($t ext, $page)


    NOTE: none of these "corrected" things will check whether
    the *contents* of the web page contain whatever is in $text
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • John Dunlop

      #3
      Re: eregi or similar_text &lt; -- problem either way

      george wrote:
      [color=blue]
      > if (eregi('/\b$text/i', '$page'))[/color]

      A couple of important points:

      (i) You're using a PCRE pattern in a POSIX function, and the
      delimiters, assertion, variable and modifier aren't
      understood.

      (ii) You're expecting variable expansion in single-quotes, in at
      least two places:
      (a) the pattern parameter, and
      (b) the subject parameter.

      Ref.:



      --
      Jock

      Comment

      • george

        #4
        Re: eregi or similar_text &lt; -- problem either way

        Thanks guys, especially Pedto, for being so lucid.

        Neither worked though.

        All I'm hoping to do is
        1. include a webpage and display it <-- which is ok
        2. Run down the page looking for a text string, and
        3. If it's there display "Match found"

        On my own attempts nothing works.

        Neither:
        if (similar_text($ text, $page)) {
        -or-
        if (eregi('\b' . $text, $page)) {

        I have a quick nasty fix running using javascript but it's ugly.
        Any further thoughts would be appreciated.

        See ya

        Comment

        • Pedro Graca

          #5
          Re: eregi or similar_text &lt; -- problem either way

          george wrote:[color=blue]
          > All I'm hoping to do is
          > 1. include a webpage and display it <-- which is ok[/color]

          Since you want the data for something more than just display it, do not
          include it, instead put its contents in a variable

          $URL = 'http://www.example.com/';
          $contents = file_get_conten ts($URL); // see [1] below
          // and now that the webpage is saved display it
          echo $contents;

          [color=blue]
          > 2. Run down the page looking for a text string, and[/color]

          $text_string = 'text string';
          if (stripos($conte nts, $text_string) !== false) { // see [2] below
          [color=blue]
          > 3. If it's there display "Match found"[/color]

          echo 'Match found';
          }


          [1] http://www.php.net/file_get_contents
          use another method to get the file contents if your php < 4.3.0
          fopen() and fread() should do it [don't forget fclose() if you
          choose to use fopen()]

          [2] http://www.php.net/stripos
          stripos() performs a case insensitive search; if you need a case
          sensitive search use strpos() instead
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • george

            #6
            Re: eregi or similar_text &lt; -- problem either way

            THANK YOU PEDRO
            ...
            Because the server is running <PHP5, I got an undefined function on stripos().
            But this did the trick:

            function stripos($conten ts, $text_string, $offset=0) {
            return strpos(strtoupp er($contents), strtoupper($tex t_string), $offset);
            }

            if (stripos($conte nts, $text_string) !== false) {
            echo "Match";
            } else {
            echo "Duh!";
            }


            Thank you very very much.
            Bye

            Comment

            Working...