php str_replace替换反斜杠,使用str_replace解析反斜杠转义

"这篇博客探讨了在使用PHP的str_replace函数时遇到的问题,主要在于无法精确控制替换操作和占位符的选择。作者提出了一种解决方法,通过生成独特的占位符并确保其不与任何输入字符串冲突,来避免替换错误。该方法适用于处理包含@转义序列的情况,并提供了两种替换模式。示例展示了如何处理包含多个替换值和复杂字符串的情况。"
摘要由CSDN通过智能技术生成

我认为仅限于使用str_replace时的主要问题是您几乎无法控制哪些字符串被替换(因为所有事件都会被替换),并且在选择占位符时需要特别小心对于\@转义序列。两个插入的值组合可能会生成占位符字符串,因此在还原占位符替换时将其转换为@字符。

以下是一种蛮力的解决方案,试图解决这个问题。它一次检查一个占位符对照模板字符串,替换值和最终字符串,确保占位符不出现在任何这些字符串中,并且最初为\@引入的占位符数与该数字匹配占位符还原。您可能希望设置一个默认的占位符,而不是xyz(如零char或其他),最适合您,以避免不必要的处理。

可以使用两种替换模式(@和@)调用它,但目前它们不能混合使用。

这不是我写过的最漂亮的代码,但鉴于str_replace约束,它仍然是我的镜头,我希望它对你有所帮助。

function templateMap ($string, $array, $defaultPlaceholder = "xyz")

{

$newArray = array();

// Create an array of the subject string and replacement arrays

$knownStrings = array($string);

foreach ($array as $subArray) {

if (is_array($subArray)) {

$knownStrings = array_merge($knownStrings, array_values($subArray));

}

else {

$knownStrings[] = $subArray;

}

}

$placeHolder = '';

while (true) {

if (!$placeHolder) {

// This is the first try, so let's try the default placeholder

$placeHolder = $defaultPlaceholder;

}

else {

// We've been here before - we need to try another placeholder

$placeHolder = uniqid('bs-placeholder-', true);

}

// Try to find a placeholder that does not appear in any of the strings

foreach ($knownStrings as $knownString) {

// Does $placeHolder exist in $knownString?

str_replace($placeHolder, 'whatever', $knownString, $count);

if ($count > 0) {

// Placeholder candidate was found in one of the strings

continue 2; // Start over

}

}

// Will go for placeholder "$placeHolder"

foreach ($array as $subArray) {

$newString = $string;

// Apply placeholder for \@ - remember number of replacements

$newString = str_replace(

'\@', $placeHolder, $newString, $numberOfFirstReplacements

);

if (is_array($subArray)) {

// Make substitution on @

for ($i = 0; $i <= 9; $i++) {

@$newString = str_replace("@$i", $subArray[$i], $newString);

}

}

else {

// Make substitution on @

@$newString = str_replace("@", $subArray, $newString);

}

// Revert placeholder for \@ - remember number of replacements

$newString = str_replace(

$placeHolder, '@', $newString, $numberOfSecondReplacements

);

if ($numberOfFirstReplacements != $numberOfSecondReplacements) {

// Darn - value substitution caused used placeholder to appear,

// ruining our day - we need some other placeholder

$newArray = array();

continue 2;

}

// Looks promising

$newArray[] = $newString;

}

// All is well that ends well

break;

}

return $newArray;

}

$a = templateMap(

"(@ and one escaped \@)",

array(" col1 < 5 && col2 > 6", " col3 < 3 || col4 > 7")

);

print_r($a);

$a = templateMap(

"You can tweet @0 \@2 @1",

array(

array("Sarah", "ssarahtweetz"),

array("John", "jjohnsthetweetiest"),

)

);

print_r($a);

输出:

Array

(

[0] => ( col1 < 5 && col2 > 6 and one escaped @)

[1] => ( col3 < 3 || col4 > 7 and one escaped @)

)

Array

(

[0] => You can tweet Sarah @2 ssarahtweetz

[1] => You can tweet John @2 jjohnsthetweetiest

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值