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?
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.