0

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">

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by diafol
0

how about str_replace()? Seems easier.

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

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.