grab succeeding string

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

    grab succeeding string

    Say I have a chunk of text and I know that somewhere in that chunk ot
    text is the phrase 'Next Week's Winning Number will be:', is there a
    way I can automatically grab the string immediately following?

    Any help greatly appreciated.

  • Rik

    #2
    Re: grab succeeding string

    strawberry wrote:[color=blue]
    > Say I have a chunk of text and I know that somewhere in that chunk ot
    > text is the phrase 'Next Week's Winning Number will be:', is there a
    > way I can automatically grab the string immediately following?
    >
    > Any help greatly appreciated.[/color]


    If you know the stringlength of the following string:

    $occurance = strpos( $chunk, 'Next Week's Winning Number will be:');
    $occurance += strlen('Next Week's Winning Number will be:');
    $string = trim(substr($oc curance, $lenght_of_sear ched_string));

    If the lenght is undefined, use slower regular expression (allthough added
    bonus is we can filter out whitespace-characters):

    preg_match("/(?<=Next Week's Winning Number will be:)\s*([^\s]+?)\b/si",
    $chunk, $match);
    $string=$match[1];

    Grtz,
    --
    Rik Wasmus


    Comment

    • Mladen Gogala

      #3
      Re: grab succeeding string

      On Wed, 10 May 2006 16:43:37 -0700, strawberry wrote:
      [color=blue]
      > Say I have a chunk of text and I know that somewhere in that chunk ot
      > text is the phrase 'Next Week's Winning Number will be:', is there a
      > way I can automatically grab the string immediately following?
      >
      > Any help greatly appreciated.[/color]

      Does the following snippet answer your question?

      #!/usr/local/bin/php
      <?php
      $buff = "Next Week's Lotto: 123456";
      if (preg_match("/Next Week's Lotto:\s*(\d+)/", $buff, $hit)) {
      echo "Next week lottery:", $hit[1], "\n";
      } else {
      echo "You suck!\n";
      }
      ?>


      Of course, I make no claims that the next week's lottery numbers will be
      "123456". No amount of regex massage will help you with that.

      --


      Comment

      • strawberry

        #4
        Re: grab succeeding string

        Thanks Rik and Mladen. That's a great help.

        This time next week, I think I'm going to be very, very lucky ;-)

        Comment

        • strawberry

          #5
          Re: grab succeeding string

          Hello again,

          For various reasons, tiny little errors in the scripts provided
          prohibited them from working (at least on my system). However, they got
          me pointed in the right direction and, coupled with a useful
          introduction to Regular Expressions at
          https://www.sunflowerroad.com/blog/t...torial-part-1/,
          I was able to put together the following. I don't know if it's perfect
          - but it works for my purposes so I thought I'd share it with everyone:

          $watchword = 'Numbers:&nbsp; ';

          if (preg_match("/(?<=$watchword) .*?\s/s",$pagedata , $match)){
          $string=$match[0];
          echo $string;

          Regards

          Comment

          Working...