php - Finding the value of an option with specific string -
consider snippet select form:
<select name="cat_id""> <option value="33">apple</option> <option value="44">banana</option> <option value="48">carrot</option> <option value="50">dew</option> <option value="77">eggplant</option> <option value="84">fern</option> <option value="92">grass</option> </select>
is there way, using simple dom, can extract value="x" specific string.
example, want value of dew, hence, must able 50 tried searching , playing, cannot find exact answer:
parsing drop down menu php simple dom <-- values doesn't start 0 nor increments uniformly
php , simple_html_dom.php, selected option <-- not have selected entry in select form
i need value specific string can use send form using curl.
hope, can help. in advance , more power.
the following code parse option
nodes , when searched text matched, display corresponding value , ends:
$input = <<<_data_ <select name="cat_id"> <option value="33">apple</option> <option value="44">banana</option> <option value="48">carrot</option> <option value="50">dew</option> <option value="77">eggplant</option> <option value="84">fern</option> <option value="92">grass</option> </select> _data_; // create dom object $html = new simple_html_dom(); // load html string $html->load($input); // searched text $searchtext = 'dew'; // create regex pattern (match $searchtext followed or preceded number of spaces/tabs/newlines ...) // flag stands case insensitive match $pattern = '/\s*'.$searchtext.'\s*/i'; foreach( $html->find('select[name="cat_id"] option') $option ){ echo $string = $option->plaintext; // check if current node contains searched text if( preg_match($pattern, $string) ){ $value = $option->value; echo ' => ' . $value; // exit loop when done break; } echo '<br>'; }
output
apple banana carrot dew => 50
Comments
Post a Comment