SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Location
    Atlanta, GA
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    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.

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

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    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 00:40.


Bookmarks

Posting Permissions

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