Php preg_replace form field

Posted on 2011-09-09
Last Modified: 2012-05-12
I think I should be able to search for an input field html and replace using the code below, where the variable $html contains a text input field with it's value set to [quantity]. However this depends on the value setting preceding the field's closing tag. Can this pattern match be improved to search for html that starts with '<input', ends with '/>' and contains 'value="[quantity]"'?
$new_html = preg_replace('/<input(.*)value="\[quantity\]" \/>/', $quantity, $html);

Open in new window

Question by:ncw
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
  • 2
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 36512065
Try this:
$new_html = preg_replace('#<input(?=[^>]*value="\[quantity\]")[^>]*>#', $quantity, $html);

Open in new window


Author Closing Comment

ID: 36521876
Yes that worked well thank you.

Author Comment

ID: 36521994
Wondered if you could help me with a similar question please at

Featured Post

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.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 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