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
  • 2
LVL 74

Accepted Solution

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

Open in new window


Author Closing Comment

Comment Utility
Yes that worked well thank you.

Author Comment

Comment Utility
Wondered if you could help me with a similar question please at

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

8 Experts available now in Live!

Get 1:1 Help Now