[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Replace value field in text boxes

Posted on 2007-08-01
Medium Priority
Last Modified: 2007-08-01
How can I use preg_replace to change this:
<input type="text" name="x" value="y">
to this:
<input type="text" name="x" value="z">

The name and value will always be different.

I tried the code below but it is just removing the entire string:
$replacement = preg_replace('value="(.*)"/U','value="'.$id.'"',$data[0][$i]);
Question by:submissiontechnology
LVL 11

Expert Comment

ID: 19607424
Ok. Here is a solution.
You've used wrong regexp.

$id = '123456';
$string = '<input type="text" name="x" value="y">';
$pat = '/value="(.*)"/';
$rep = 'value="'.$id.'"';
$replaced = preg_replace($pat,$rep, $string);

I hope this will help
Best Regards

LVL 17

Accepted Solution

Aleksandar Bradarić earned 2000 total points
ID: 19607439
Have you tried this:
$replacement = preg_replace('/value="(.*)"/U','value="'.$id.'"',$data[0][$i]);

Author Comment

ID: 19607508
I must be blind as a bat for not spotting the missing /
Thanks leannonn!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 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