带字符串的通用 PHP 匹配模板
是否有一个函数可以让我执行以下操作:
$template = "{name}:{city}-{state}"
$string = "Tom:Some CityPlace-CA"
$out = function_I_am_looking_for($template,$string);
当 $out 返回时
Array(
[name] => Tom
[city] => Some CityPlace
[state] => CA
)
这样的函数存在吗?
-------- 编辑 -----------
所以人们已经发言了,既然我不喜欢看到这样的东西消亡,我就得出结论。不存在内置函数,但是我模拟了一个,并且它确实有效。请随意完善,请评论您的编辑。
function genaric_match($template,$string,$varStart="{{",$varEnd="}}"){
$template = str_replace($varStart,"|~|`",$template);
$template = str_replace($varEnd,"`|~|",$template);
$t=explode("|~|",$template);
$temp="";
$i=0;
foreach ($t as $n=>$v){
$i++;
if (($i==count($t)||($i==(count($t)-1)&&$t[$n+1]==""))&&substr($v,0,1)=="`"&&substr($v,-1)=="`"){
//Last Item
$temp.="(?P<".substr($v,1,-1).">.++)";
}elseif(substr($v,0,1)=="`"&&substr($v,-1)=="`"){
//Search Item
$temp.="(?P<".substr($v,1,-1).">[^".$t[$n+1]."]++)";
}else{
$temp.=$v;
}
}
$temp="~^".$temp."$~";
preg_match($temp, $string, $matches);
return $matches;
}
此示例
print_r(genaric_match("{{name}}:{{city}}-{{state}}","Tom:Some CityPlace-CA"));
返回
Array
(
[0] => Tom:Some CityPlace-CA
[name] => Tom
[1] => Tom
[city] => Some CityPlace
[2] => Some CityPlace
[state] => CA
[3] => CA
)
is there a function that will allow me to do the following:
$template = "{name}:{city}-{state}"
$string = "Tom:Some CityPlace-CA"
$out = function_I_am_looking_for($template,$string);
when $out returns
Array(
[name] => Tom
[city] => Some CityPlace
[state] => CA
)
Does such a function exist?
-------- EDIT -----------
So the people have spoken, and since I dont like seeing something like this die, I will conclude. No built in function exists, however I did mock up one, and it does work. Feel free to refine, please comment you edits.
function genaric_match($template,$string,$varStart="{{",$varEnd="}}"){
$template = str_replace($varStart,"|~|`",$template);
$template = str_replace($varEnd,"`|~|",$template);
$t=explode("|~|",$template);
$temp="";
$i=0;
foreach ($t as $n=>$v){
$i++;
if (($i==count($t)||($i==(count($t)-1)&&$t[$n+1]==""))&&substr($v,0,1)=="`"&&substr($v,-1)=="`"){
//Last Item
$temp.="(?P<".substr($v,1,-1).">.++)";
}elseif(substr($v,0,1)=="`"&&substr($v,-1)=="`"){
//Search Item
$temp.="(?P<".substr($v,1,-1).">[^".$t[$n+1]."]++)";
}else{
$temp.=$v;
}
}
$temp="~^".$temp."$~";
preg_match($temp, $string, $matches);
return $matches;
}
This example
print_r(genaric_match("{{name}}:{{city}}-{{state}}","Tom:Some CityPlace-CA"));
Returns
Array
(
[0] => Tom:Some CityPlace-CA
[name] => Tom
[1] => Tom
[city] => Some CityPlace
[2] => Some CityPlace
[state] => CA
[3] => CA
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它叫做
preg_match
。但显然你需要为其编写一个正则表达式。 (如果您的问题是,是否存在神奇的识别任何模式而无需知道它的语法功能,那么答案是:不。)Yes, it's called
preg_match
. But you obviously need to write a regex for it. (If your question is, whether there is a magic recognize-any-pattern-without-even-knowing-it's-syntax function, then the answer is: No.)PHP 中没有“开箱即用”的此类函数,但编写一个函数相当简单,因为有很多函数,例如...
。 ..您可以用它来创建一个。
然而,不得不说的是,使用 PHP(很大程度上是一种模板语言)来创建另一种模板语言似乎是一个奇怪的选择。
There's no such function in PHP "out of the box", but it would be fairly trivial to write one, as there are quite a few functions such as...
...that you could use to create one.
However, it has to be said that using PHP (which is to a large extent a template language) to create another template language seems an odd choice.