SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Atlanta, GA
    0 Post(s)
    0 Thread(s)

    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.

    <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! (Open Beta Phase)
    OnApp Powered Linux & Windows Cloud Hosting[/COLOR]

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    0 Post(s)
    0 Thread(s)

    Try this:

    preg_match_all("|value=\"(.+)\">(.+)<|", $html_doc, $matches);

    $matches is a two dimensional array. $matches[0][x] contains the full pattern matches, which you don't care about. $matches[1][x] contains the matches to the first parenthesized substring of the pattern i.e. the part in the first set of parentheses, which will be your value attribute. $matches[2][x] will contain the matches to the second parenthesized substring of the pattern, which will be your "Hello" part.

    Note: the above requires perfect html coding like you posted. If there could be some spaces at certain places in the html that the pattern looks at or there might not be any spaces, then you need to add a blank followed by a "*" to the pattern wherever that may occur.
    Last edited by 7stud; May 28, 2001 at 01:40.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts