开发者

preg_match_all for <option></option> to get all matching values

开发者 https://www.devze.com 2023-01-30 12:25 出处:网络
Here is the string. 开发者_StackOverflow$string = <option class=\"abcd\" value=\"1.5\">Some Text with White Spaces</option>

Here is the string.

开发者_StackOverflow$string = <option class="abcd" value="1.5">Some Text with White Spaces</option>

I want to get all matching variables (1.5 & Some Text with White Spaces) using preg_match_all or something similar. I was doing this a long ago but I don't remember it now. Hope you can help...


I couldn't answer a html & regex question on Stack Overflow without mentioning please use a parser.

However, if you were really keen on a regex, you could see below.


If your strings look like that, then you could use...

preg_match_all('/<option\sclass="[^"]*"\svalue="([^"]*)">([^>]*)<\/option>/', $string, $matches);

var_dump($matches);

$matches[1] will have the value attribute and $matches[2] will have the text node.

0

精彩评论

暂无评论...
验证码 换一张
取 消