目前,我正在寻找一种从String调用方法的方法。我的字符串看起来像这样:
"Hello, here's the Link you look for: [[Link,internLink,Go,Login]]."
我想替换[[Link,internLink,Go,Login]]
为:
K :: gI('Link')->internLink('Go', 'Login');
有办法吗?原因是,我在数据库中保存了各种文本,因此需要在这些文本中调用方法。同样,只有第一个参数(这里:Link和internLink)始终显示为类和方法。在这两个参数之后,根据方法的不同,可能有0-XXX个参数。我太紧张了。
编辑:我试图与preg_replace一起工作,但如有必要,我愿意采用一种全新的方式!
<?php
$subject = "Hello, here's the Link you look for: [[Link,internLink,Go,Login]].";
$result = preg_replace_callback(
'/\[\[(.*?)]\]/',
function ($matches) {
$args = explode(",", $matches[1]);
$args = array_map("trim", $args);
$x1 = !empty($args[0])?$args[0]:"default";
$x2 = isset($args[1])?$args[1]:"default";
$x3 = "";
$args = array_slice($args, 2);
if (count($args) > 0) {
$x3 = "'" . implode("', '", $args) . "'";
}
return "K :: gI('$x1')->$x2($x3);";
},
$subject
);
echo "$result\n";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句