我想要的只是获取两个字符串之间的内容,就像下面这行:
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
我希望获得81L
和82R
之间的所有内容,然后通过Preg_match将它们自动编码为Base64,我认为,我已经做了一些方法来做到这一点,但没有得到预期的结果!
基本形式:
81Lhello82R 81Lmy82R 81Lwife82R
输出:81LaGVsbG8=82R 81LbXk=82R 81Ld2lmZQ==82R
硬性规定:
$leftMask = '81L';
$rightMask = '82R';
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
preg_match_all('#'.$leftMask.'(.*)'.$rightMask.'#U',$content, $out);
$output = [];
foreach($out[1] as $val){
$output[] = $leftMask.base64_encode($val).$rightMask;
}
$result = str_replace($out[0], $output, $content);
正则表达式规则
$leftMask = ''d{2}L';
$rightMask = ''d{2}R';
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
preg_match_all('#('.$leftMask.')(.*)('.$rightMask.')#U',$content, $out);;
$output = [];
foreach($out[2] as $key=>$val){
$output[] = $out[1][$key].base64_encode($val).$out[3][$key];
}
$result = str_replace($out[0], $output, $content);
preg_replace_callback:
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
$output = preg_replace_callback(
'/(?<='b'd'dL)(.+?)(?='d'dR)/',
function($matches) {
return base64_encode($matches[1]); // encode the word and return it
},
$content);
echo $output,"'n";
,
-
(?<='b'd'dL)
是一个积极的向后看,确保我们有2个数字和字母L在单词前编码 -
(?='d'dR)
是一个积极的前瞻性,确保我们有2个数字和字母R后的单词编码 -
(.+?)
是包含要编码的字的捕获组
81LaGVsbG8=82R 81LbXk=82R 81Ld2lmZQ==82R