0

I have a string like:$str='&%#^*@\"~ \'a4{=s{sa*}7s*&$db{abc654d3ws67}d(*$%#$c6'#^*@"~ \'a4\"'; .

I need to find what is not like {abc654d3ws67} as $needle from that string and preplace it by bin2hex($needle).

Example: bin2hex('#').

4

2 回答 2

3

查找 的每次出现{,后跟任意数量的字母或数字,然后是}

$str = preg_replace_callback( '/\{([^a-z0-9]+)\}/i', function( $match) { 
    return bin2hex( $match[1]); 
}, $str);
于 2013-05-24T02:17:52.647 回答
2

根据措辞,这听起来像是您正在寻找的内容:

<?php

$str='&%#^*@\"~ \'a4{=s{sa*}7s*&$db{abc654d3ws67}d(*$%#$c6\'#^*@"~ \'a4\"';
$pattern = '!^(.+)({abc654d3ws67})(.+)$!';
$tstring = preg_match($pattern,$str,$matches);
$newstring = bin2hex($matches[1]).$matches[2].bin2hex($matches[3]);

echo "<pre>$newstring</pre>";
?>

输出是:

2625235e2a405c227e202761347b3d737b73612a7d37732a26246462{abc654d3ws67}64282a24252324633627235e2a40227e202761345c22

旧代码引发了 T_LNUMBER 警告。

更新仅适用于十六进制:

<?php

$str='&%#^*@\"~ \'a4{=s{sa*}7s*&$db{abc654d3ws67}d(*$%#$c6\'#^*@"~ \'a4\"';
$pattern = '!^(.+)(abc654d3ws67)(.+)$!';
$tstring = preg_match($pattern,$str,$matches);
$newstring = bin2hex($matches[1]).$matches[2].bin2hex($matches[3]);

echo "<pre>$newstring</pre>";
?>

输出是:

2625235e2a405c227e202761347b3d737b73612a7d37732a26246462abc654d3ws6764282a24252324633627235e2a40227e202761345c22

于 2013-05-24T02:28:06.930 回答