PDA

View Full Version : String Manipulation & Pattern Matching


alpha
Sun 27th May '01, 1:05pm
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!

Oasis
Sun 27th May '01, 1:21pm
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.

alpha
Sun 27th May '01, 2:44pm
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

alpha
Sun 27th May '01, 3:19pm
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! :)

Matt@ikonboard
Fri 1st Jun '01, 11:38am
In Perl:

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

Freddie Bingham
Fri 1st Jun '01, 11:58am
Taking <option value="option1">Hello</option>

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 :)