deckerdev
deckerdev

Reputation: 2792

Replace an <input type="submit"> button with an <a> which executes javascript when clicked

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?

Upvotes: 0

Views: 253

Answers (2)

mario
mario

Reputation: 145512

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.)

Upvotes: 1

evbailey
evbailey

Reputation: 371

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 "/>".

Upvotes: 1

Related Questions