如何用PHP中的给定值替换正则表达式?
我有一个正则表达式列表,比如如何用PHP中的给定值替换正则表达式?,php,regex,Php,Regex,我有一个正则表达式列表,比如 $regex = "{Hello ([a-zA-Z]+), you are ([0-9]{2}) years old today\.}u" 是否有执行以下操作的函数: $result = function_i_am_looking($regex, "John", 25); echo $result; // Outputs : "Hello John, you are 25 years old today." 注意:这不是我正在构建的某种模板引擎;) 注2:我无法
$regex = "{Hello ([a-zA-Z]+), you are ([0-9]{2}) years old today\.}u"
是否有执行以下操作的函数:
$result = function_i_am_looking($regex, "John", 25);
echo $result; // Outputs : "Hello John, you are 25 years old today."
注意:这不是我正在构建的某种模板引擎;)
注2:我无法预测正则表达式将是什么以及以什么顺序出现。您可能需要使用正则表达式
将打印:
Hello John, you are 25 years old today.
您可以使用一些混合子字符串来替换它们
$your_string = "Hello @##@YOUR_NAME@##@, you are @##@YOUR_AGE@##@ years old today.";
$new_string = get_full_string($your_string, "John", 25);
echo $new_string;
function get_full_string($str, $name, $age)
{
$str = str_replace("@##@YOUR_NAME@##@", $name, $str);
$str = str_replace("@##@YOUR_AGE@##@", $age, $str);
return $str;
}
方法:2
$your_string = "Hello ([a-z]+), you are ([0-9]{2}) years old today.";
$new_string = get_full_string($your_string, "John", 25);
echo $new_string;
function get_full_string($str, $name, $age)
{
$str = str_replace("([a-z]+)", $name, $str);
$str = str_replace("([0-9]{2})", $age, $str);
return $str;
}
怎么样:
$regex = "{Hello ([a-z]+), you are ([0-9]{2}) years old today.}u";
$result = function_i_am_looking($regex, "John", 25);
echo $result;
function function_i_am_looking($reg, $str, $num) {
$reg = preg_replace('/\(\[a-z\].*?\)/', '%s', $reg);
$reg = preg_replace('/\(\[0-9\].*?\)/', '%d', $reg);
return sprintf($reg, $str, $num);
}
如果限制匹配组不嵌套在正则表达式模式中,则可以使用以下方法:
$regex = "/Hello ([a-z]+), you are ([0-9]{2}) years old today./u";
$replacements=array("John", 25);
$result = preg_replace_callback('/\((.*?)\)/', function($m) use (&$replacements) {
return array_shift($replacements);
}, $regex);
echo $result; // Outputs : "Hello John, you are 25 years old today."
在我看来,这已经是你能做的最好的事情了。然而,虽然它有效(有点:),但我认为这样做不是一个好主意。您需要的是模板引擎,甚至是printf(是的,您需要;)您的
$regex
不是有效的PRCE regex。最接近的是sprintf
用于从格式写入字符串,但这和一个女人是两码事regex@t3chguy哪里错了?@hek2mgl:我在考虑用给定的参数按特定顺序替换每个括号。@t3chguy{}
允许在PHP中使用正则表达式(实际上,任何非字母数字字符;),谢谢你的建议,但我已经在使用正则表达式了,我无法将其更改为sprintf
可接受的格式:/除非您可以采用另一种方式,假设您在一个数组中有多个句子,包括示例中的一个,我给您文本“你好,乌姆,您今天14岁了。”
,您能将其与匹配吗“您好%s,您今天是%i岁。”
?@cyrin。不,我看不出有任何方法可以做到这一点。(使用sprintf格式说明符)谢谢,但正则表达式系统已经就位,我无法更改它,除非您可以使它双向工作(请查看我对@wumm-answer的评论)谢谢你的建议。我可能遗漏了一点:我无法预测正则表达式结构(哪些参数和顺序)。谢谢你的建议。我可能遗漏了一点:我无法预测正则表达式结构(哪些参数和顺序).printf
,比如sprintf
很有趣,但我不能让它们反过来工作(匹配“Hello hek2mgl,你今天37岁了。”)…除非我更改原始preg\u替换为将“%s”替换为(\w+),etcLike也可以使用匿名函数;)@西里尔。不要对这个解决方案期望太高。我只能说,您不是第一个寻找将正则表达式用作a)解析器b)格式说明符的方法的人。我想记得一个用C写的项目。让我再帮你找一次…@CyrilN。检查这个。。。这和你想要的不完全一样,但是如果你需要,你可以决定这个或者至少他们的想法。还有相关的项目,强调你的谷歌。祝你好运很好,这很有趣!谢谢:)
$regex = "/Hello ([a-z]+), you are ([0-9]{2}) years old today./u";
$replacements=array("John", 25);
$result = preg_replace_callback('/\((.*?)\)/', function($m) use (&$replacements) {
return array_shift($replacements);
}, $regex);
echo $result; // Outputs : "Hello John, you are 25 years old today."