简体   繁体   English

php正则表达式

[英]php regular expression

I have this tag in a php string: 我在php字符串中有这个标签:
<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. 我很确定我需要使用php函数preg_replace但我不确定正则表达式。 Is it possible to make a regular expression that will replace all <input type="submit" /> tags regardless of what additional properties the tag has? 是否可以创建一个正则表达式来替换所有<input type="submit" />标记,而不管标记具有哪些附加属性?

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 "/>". 它捕获所有不是“/”的字符,直到尾部的“/>”。

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.) (查看一些正则表达式设计工具以满足未来需求。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM