带字符串的通用 PHP 匹配模板

发布于 2024-10-12 18:44:14 字数 1544 浏览 7 评论 0原文

是否有一个函数可以让我执行以下操作:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-10-19 18:44:14

是的,它叫做 preg_match。但显然你需要为其编写一个正则表达式。 (如果您的问题是,是否存在神奇的识别任何模式而无需知道它的语法功能,那么答案是:不。)

preg_match('~^(?P<name>[^:]++):(?P<city>[^-]++)-(?P<state>.++)$~', $string, $matches);
var_dump($matches);

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

preg_match('~^(?P<name>[^:]++):(?P<city>[^-]++)-(?P<state>.++)$~', $string, $matches);
var_dump($matches);
难以启齿的温柔 2024-10-19 18:44:14

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文