I have a string "<select><option value = 'LB1'></option></select>"
What I want to do is replace only the <select> with <select name = "somename">. Currently,
i have a preg_replace statement that replaces the </select> statement also. How can I prevent this.

My current code is

$string = "<select><option value = 'LB1'></option></select>"
$pattern = '<select>';
$replacement = "select name = 'somename' ";
$semistring = preg_replace($pattern, $replacement, $string);

echo "$semistring";

The expected result Is
<select name = "somename"><option value = 'LB1'></option></select>

but I get the following result
<select name = "somename"><option value = 'LB1'></option></select name = "somename">

Member Avatar for diafol

how about str_replace()? Seems easier.

str_replace ( "<select>" , '<select name="somename">' , $string );

how about str_replace()? Seems easier.

str_replace ( "<select>" , '<select name="somename">' , $string );

Thank you very much. It really worked. I had to make a minor modification to it. The replacement is actually <select name = '$somename'> where $somename is a variable that will assign the value.
so the final statement looked like this

str_replace ( "<select>" , "<select name='$somename'>" , $string );

Thanks again for your help. I have put up one more question on substring selection. Can you please help me with that? That would be very helpful.
The thread is http://www.daniweb.com/web-development/php/threads/365081/1564209#post1564209

Member Avatar for diafol

Ok, if solved, mark it solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.