Replace value field in text boxes

Posted on 2007-08-01
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

    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

    Have you tried this:
    $replacement = preg_replace('/value="(.*)"/U','value="'.$id.'"',$data[0][$i]);

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    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…
    The viewer will learn how to dynamically set the form action using jQuery.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now