0

I have this tag in a php string:
<input type="submit" name="op" id="edit-submit" value="Log in" class="form-submit ajax-trigger" />
that I'd like to replace with
<a href="#" onclick="document.user-login.submit();" class="sign-in">sign in</a>

I'm pretty sure that I need to use the php function preg_replace but I'm not sure of the regular expression. Is it possible to make a regular expression that will replace all <input type="submit" /> tags regardless of what additional properties the tag has?

2
  • 1
    Rather depends what you want to replace it with, what the attributes contain and how well formed your XHTML is. Commented Mar 9, 2011 at 0:13
  • Take care the user-login is an invalid tag id; you need an underscore there. Commented Mar 9, 2011 at 0:25

2 Answers 2

1

That aim is simplistic enough for a regex to work. In your case:

 $text = preg_replace('#<input\b[^>]*\s(type="submit")[^>]*>#i',
                      '<a href="#" onclick=".....', $text);

(Check out some of the regex design tools for future needs.)

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following pattern to catch all input tags as you described. It will strip out the additional properties of the tag, which I think is what you're asking for.

/<input type="submit"[^/]+\/>/

I didn't test this myself, but it should get you going in the right direction. The [^/]+ is the key; it catches all characters that are NOT "/" up to the trailing "/>".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.