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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

734 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