Php preg_replace textarea field with a value

The code below searches for an input field with a specified id value and replaces the whole field with some text. How can I do the same for a textarea field which has a </textarea> closing tag?
$html = preg_replace('#<input(?=[^>]*id="some_field_id_value")[^>]*>#', 'some_text', $html);

Open in new window

LVL 1
ncwAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NoiSConnect With a Mentor Commented:
Try with

<?php
echo = preg_replace('#<textarea[^>]*>(.*?)</textarea>#is','\\1some_text',$html);

Open in new window


if you need to specify the textarea using an ID...
<?php
echo = preg_replace('#<textarea[^>]*id="id_value".*?>(.*?)</textarea>#is','\\1some_text',$html);

Open in new window

0
 
käµfm³d 👽Commented:
Try this:

$html = preg_replace('#(<textarea(?=[^>]*id="some_field_id_value")[^>]*>)(?:[^<]|<(?!/textarea>))*#i', '$1some_text', $html);

Open in new window


Be sure to leave the "$1" in the replacement, but you are free to change the value of "some_text".
0
 
ncwAuthor Commented:
That doesn't seem to remove the closing </textarea> tag.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ncwAuthor Commented:
In the replacement text is the \\1 important?
0
 
NoiSCommented:
Only if you need to use the text inside <textarea></textarea>,
otherwise, you can remove it safely, meaning that all <textarea> will be replaced by $some_text
0
 
käµfm³d 👽Commented:
That doesn't seem to remove the closing </textarea> tag.
I thought you only wanted to change the value of the <textarea>. Are you saying you want to remove it completely?
0
 
ncwAuthor Commented:
Yes, in the question I say 'replaces the whole field'.
0
 
käµfm³d 👽Commented:
Ah. Then you have your answer.
0
 
ncwAuthor Commented:
Yes it's working thanks.
0
 
ncwAuthor Commented:
0
All Courses

From novice to tech pro — start learning today.