Solved

Php preg_replace textarea field with a value

Posted on 2011-09-12
10
1,039 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

856 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