假设我有以下字符串:
"my string ? other string ?"
我要替换第一个“?”与"first param ?"
(注意在文本内部的占位符?)
第二个"second param".
如果我做一个preg_replace我得到这个:
my string first param second param other string ?
^^^^^^^^^^^^^^^^^^^^^^^^ ^
WRONG NOT REPLACED
基本上,因为第一个替换项还具有占位符,所以preg_replace
足够愚蠢,无法替换该占位符,而不是最后一个真正的第二个占位符。
preg_replace
代码:
$search = ["?", "?"];
$params = ["first param ?", "second param"];
$query ="first text ? other text ?";
//> Marker in the query are ?, so I create the array to preg_replace
$search = array_fill(0,count($params),'/\?/');
$query = preg_replace(
$search, // a list of ?
$params, // escaped values
$query, // from query
1 // replace only 1 time
);
//output: first text first param second param other text ?
关于如何避免在替换中搜索占位符的任何提示?
使用preg_replace
的实时代码:http://sandbox.onlinephpfunctions.com/code/e705ba454d030103344bc826e0fe0bf42d5b7b90
在str_replace
下也不起作用
$search = ["?", "?"];
$params = ["first param ?", "second param"];
$query ="first text ? other text ?";
$query = str_replace ($search, $params, $query);
echo $query;
// output: first text first param second param other text first param second param
使用str_replace的实时代码:
http://sandbox.onlinephpfunctions.com/code/dc259325411ee42de759f145eac78b339f329f74
预期输出
鉴于:
$search = ["?", "?"];
$params = ["first param ?", "second param"];
$query ="first text ? other text ?";
预期的输出是:
first text first param ? other text second param
^^^^^^^^^^^^^ ^^^^^^^^^^^^
first placeholder second placeholder
带有3个参数的输出除外
$search = ["?", "?", "?"];
$params = ["first param", "second param ?", "third param"];
$query ="first text ? other text ? other chunk ?";
预期的输出是:
first text first param other text second param ? other chunk third param
^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^
first placeholder second placeholder third placeholder
我的自定义解决方案
我已经提出了使用preg_split
的可能解决方案,但是说实话,这是如此棘手,必须有更好的东西:
$parts = preg_split('/(\?)/', $query, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($parts as $k=>&$v) {
// if is odd, then it's a placeholder
if ($k%2 == 1)
$v = $params[$k/2]; // replace placeholder with a param
}
$query = implode('',$parts);
参考方案
任何自定义替换逻辑都应使用preg_replace_callback
实现,例如:
$params = ["first param", "second param ?", "third param"];
$query ="first text ? other text ? other chunk ?";
echo preg_replace_callback('/\?/', function($m) use (&$params) {
return array_shift($params);
}, $query);
实时代码:http://sandbox.onlinephpfunctions.com/code/33f4804b49103e54e8070e8d9959ec9642930857
PHP:获取调用引用的数组名称 - php假定以下函数并调用:function doSomething( &$someArray ) { // Do something to $someArray } $names=array("John", "Paul", "George", "Ringo"); doSomet…
PHP-MySQL结果转换为JSON - php我试图了解如何将MySQL结果转换为JSON格式,以便以后可以在Javascript中使用此JSON来构建HTML表。但是我的代码只是产生大量的空值,我还不明白为什么。$result = mysqli_query($con, "SELECT * FROM Customers"); $test = json_encode($result);…
PHP Count数组元素 - php嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…
PHP:从函数返回值并直接回显它? - php这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…
PHP:将数据从二维数组复制到一维数组的最快方法 - php我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…