帮我学习PHP的正则表达式 [英] Help me with a regular expression for PHP
问题描述
我不知道在哪里可以获得RE的帮助。因为它是一个PHP应用程序
我想我会问这里是否有一些RE专业人士。基本上
我正在做一些模板的东西我想用
preg_replace_callback函数调用另一个函数时
的标准RE表达式匹配,但不知道如何实现它。
所以我从这开始:
/< (输入|选择| textarea的)[^>] * name\s * \ = \s * \" [_ A-ZA-Z0-9\s] * \" [^> ] *> /
但是需要修改它才能匹配如果它有''{''字符在
名称但不匹配如果没有。
所以这不符合:
< input name =" test">
但这符合:
< input name =" test {0}">
非常感谢。
I have no idea where to get help on RE stuff. Since it''s for a PHP app
I thought I would ask here to see if there was some RE pros. Basically
I''m doing some template stuff and I wanted to use a
preg_replace_callback function to call another function when the
criteria of the RE expression is matched but have no idea how to
accomplish it.
So I start with this:
/<(input|select|textarea)[^>]*name\s*\=\s*\"[_a-zA-Z0-9\s]*\"[^>]*>/
but need to modify it so it only matches if it has ''{'' characters in
the name but to not match if it does not.
So this would not match:
<input name="test">
But this would match:
<input name="test{0}">
Thanks much in advance.
推荐答案
cendrizzi写道:
cendrizzi wrote:
所以我从这开头:
/<(输入|选择| textarea)[^>] * n ame \s * \ = \ s * \" [_ a-zA-Z0-9 \ t] * \" [^>] *> /
So I start with this:
/<(input|select|textarea)[^>]*name\s*\=\s*\"[_a-zA-Z0-9\s]*\"[^>]*>/
你最好不要使用正则表达式来验证HTML。
以下行是完全有效的HTML(我认为在任何版本中)
>
< input type =" text"名称=英寸×>< Y" id =" xy">
You''d better not use regular expressions to validate HTML.
The following line is perfectly valid HTML (I think in any version)
<input type="text" name="x><y" id="xy">
但需要对其进行修改,以便只有在
$ b $中有''{'字符时才匹配b名称但如果不匹配则不匹配。
所以这不匹配:
< input name =" test">
但这符合:
< input name =" test {0}">
but need to modify it so it only matches if it has ''{'' characters in
the name but to not match if it does not.
So this would not match:
<input name="test">
But this would match:
<input name="test{0}">
获取名称。验证它有''{''和''}''(按此顺序只有一次?)
<?php
Get the name. Verify it has ''{'' and ''}'' (in that order and once only?)
<?php
>
name = get_name(''< input name =" test {0}">''); //''test {0}''
if(name_is_valid(
name = get_name(''<input name="test{0}">''); // ''test{0}''
if (name_is_valid(
name)){
//无论什么
}
函数get_name(
name)) {
// whatever
}
function get_name(
这篇关于帮我学习PHP的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!