我正在创建一个函数,用预定义的常量替换字符串的某些部分,为此我创建了这个函数:
<?php
function ReplaceWithPath($string)
{
$toReplace = ["~~BASEPATH~~" => ABSPATH, "~~LIBPATH~~" => LIBPATH];
foreach ($toReplace as $replace => $replacement)
{
if (preg_match($replace, $string))
return str_replace($replace, $replacement, $string);
else
continue;
}
return false;
}
?>
但由于某种原因,当我使用它时,它没有给我正确的结果,而是 boolean false
。
我在配置文件中使用该函数,如下所示:
<?php
$cfg_file = str_replace("\\", "/", dirname(__FILE__)) . "/web.config.xml";
$cfg = simplexml_load_file($cfg_file);
define("ABSPATH", $cfg->paths->basepath);
require_once 'library/functions/ReplaceWithString.php';
define("LIBPATH", ReplaceWithPath($cfg->paths->libpath));
var_dump(LIBPATH);
?>
有问题的 XML 文件部分是这样的:
<paths>
<basepath>E:/projects/php/site/site/</basepath>
<libpath>~~BASEPATH~~library/</libpath>
<classpath>~~LIBPATH~~class/</classpath>
<traitpath>~~LIBPATH~~trait/</traitpath>
</paths>
我需要能够用 ~~BASEPATH~~
更改 ABSPATH
的部分,但它只返回 boolean false
。
编辑
经过测试,我发现数组( $toReplace
)中有多个常量导致此功能不起作用。为什么这样做?!?!?!
创建新函数:
function ReplacePathPart($path)
{
$replaced = "";
$toReplace = array(
'!!BASEPATH!!'=>ABSPATH,
'!!LIBPATH!!'=>LIBPATH
);
foreach ($toReplace as $replace => $replacement)
{
$replaced = str_replace($replace, $replacement, $path);
}
return $replaced;
}
修改后的配置文件:
<?php
$cfg_file = str_replace("\\", "/", dirname(__FILE__)) . "/config/web.config.xml";
$cfg = simplexml_load_file($cfg_file);
define("ABSPATH", $cfg->paths->basepath);
require_once 'library/functions/ReplaceWithString.php';
define("LIBPATH", ReplacePathPart($cfg->paths->libpath));
define("CLASSPATH", ReplacePathPart($cfg->paths->classpath));
print ABSPATH . " |abspath<br />";
print LIBPATH . " |libpath<br />";
print CLASSPATH . " |classpath<br />";
?>
输出:
E:/projects/php/site/site/ |abspath
!!BASEPATH!!library/ |libpath
!!BASEPATH!!library/class/ |classpath
最佳答案
看来你想太多了。喝一杯咖啡(☕)或阅读这个答案应该可以解决问题:
首先关闭你的原始函数:
function ReplaceWithPath($string)
{
$toReplace = ["~~BASEPATH~~" => ABSPATH, "~~LIBPATH~~" => LIBPATH];
foreach ($toReplace as $replace => $replacement)
{
if (preg_match($replace, $string))
return str_replace($replace, $replacement, $string);
else
continue;
}
return false;
}
在您的 preg_match()
中调用 ~
将被视为正则表达式字符串的分隔符。所以在你的字符串中:~~BASEPATH~~
, ~~LIBPATH~~
然后你就有了未转义的分隔符, which means your regex will fail .
return
foreach 循环中的语句将立即停止函数调用,并且仅替换 1 search => replace
元素。
不需要搜索搜索字符串并循环替换数组。由于 str_replace()
您已经搜索了搜索字符串+您可以将数组传递给函数:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
现在,在第二次尝试中,您更接近解决方案,但仍然会犯一些逻辑错误:
function ReplacePathPart($path)
{
$replaced = "";
$toReplace = array(
'!!BASEPATH!!'=>ABSPATH,
'!!LIBPATH!!'=>LIBPATH
);
foreach ($toReplace as $replace => $replacement)
{
$replaced = str_replace($replace, $replacement, $path);
}
return $replaced;
}
您仍然循环遍历替换数组,现在您使用 $path
作为永远不会改变的源,并且在每次迭代中您都会覆盖 $replaced
与替换的字符串。所以最后你只需要替换最后一个 search => replace
元素。
解决您的问题很简单:
function ReplacePathPart($path)
{
$toReplace = array(
'!!BASEPATH!!'=>ABSPATH,
'!!LIBPATH!!'=>LIBPATH
);
return str_replace(array_keys($toReplace), array_values($toReplace), $path);
}
正如我所说str_replace()
可以将数组作为参数,因此使用 array_keys($toReplace)
您搜索您的键( !!BASEPATH!!
, !!LIBPATH!!
)并将其替换为值( ABSPATH
, LIBPATH
),您可以使用 array_values($toReplace)
获得该值.
关于php - 创建一个函数来替换从 XML 文件获取的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138098/