Solved

Php preg_replace textarea field with a value

Posted on 2011-09-12
10
1,060 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ncw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36522019
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
 
LVL 1

Author Comment

by:ncw
ID: 36528514
That doesn't seem to remove the closing </textarea> tag.
0
 
LVL 11

Accepted Solution

by:
NoiS earned 500 total points
ID: 36529341
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ncw
ID: 36529431
In the replacement text is the \\1 important?
0
 
LVL 11

Expert Comment

by:NoiS
ID: 36529453
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36529968
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
 
LVL 1

Author Comment

by:ncw
ID: 36530126
Yes, in the question I say 'replaces the whole field'.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36530467
Ah. Then you have your answer.
0
 
LVL 1

Author Closing Comment

by:ncw
ID: 36530526
Yes it's working thanks.
0
 
LVL 1

Author Comment

by:ncw
ID: 36543445
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Difference between PHPClasse.php and SimplXLS.php 5 47
How do I post more than 1 item to php backend 24 60
JSON decode 5 46
Convert complicated date to yyyy-mm-dd format 22 53
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question