Solved

Php preg_replace textarea field with a value

Posted on 2011-09-12
10
1,083 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 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