Announcement

Collapse
No announcement yet.

String Manipulation & Pattern Matching

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • String Manipulation & Pattern Matching

    hey guys/gals,

    I am trying to do this but I am having a tough time getting it to work right.

    ok, i have loaded an html document into a variable:
    $html_doc = "html document here loaded from file";

    now, I want to take out parts of that HTML document. lets say within that HTML document, there is the following string.

    Code:
    <option value="option1">Hello</option>
    <option value="option2">Today</option>
    <option value="option3">is</option>
    <option value="option4">a</option>
    <option value="option5">Sunday</option>
    what I want to do is just take out the values (ie. "option1", "option2" etc) of each option and store them in an array and their corresponding names (ie. "Hello", "Today", "is", "a", "Sunday") and store them in another array.

    I think this requires pattern matching which I have a hard time doing... if anyone can help me, ill be in your debt!

    thanks!

  • #2
    Heres the pattern that I would use in Perl (not sure about PHP):

    m~<option value="(.+?)">(.+?)</option>~

    You would then reference the option value as $1
    and the names as $2

    I'm sure it would be very similar for PHP.

    Comment


    • #3
      thanks for the reply, im still working on that one...

      but another quick question...

      in PHP, how do you match the pattern that would require that the string has at least X number of commas (,) ?

      or

      the string starts with a letter (but not a number) and then the rest of the string can be whatever

      thanks
      Last edited by alpha; Sun 27 May '01, 9:58am.

      Comment


      • #4
        ahhhhh, ok... figured out my second question... but if someone can help me with the first one... like how i can add the two things into two arrays, ill appreciate it!

        Comment


        • #5
          In Perl:

          push @opt_name, $1 while $html_doc =~ m#<option value=['"](.+?)['"]>#g;
          push @opt_value, $1 while $html_doc =~ m#["']>(.+?)</option>#g;

          Comment


          • #6
            Taking <option value="option1">Hello</option>
            Code:
            preg_match_all("/<option value=\"(.*?)\">(.*?)<\/option>/si", $data, $matches);
            while (list($key,$value)=each($matches[1])) {
              $list[$matches[2][$key]] = $value;
              // Should be equal to $list[Hello] = "option1";
            }
            That might work

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X