与 php 一起使用base64_decode时,结果会有所不同


Result is different when use base64_decode with php

我在base64编码和解码方面遇到了一些问题,我想用php解码我的字符串,我找到了一个解决方案,但它效果不佳,我的期望我只想解码已编码的字符串和其他未编码的字符串应该保持正常。

这是一个字符串:

$string  = "I need your help 4Z6B4Z+S4Z6b4Z624Z+G4Z6E4oCL4Z6O4Z624Z6f4Z+L4oCL";

这是我尝试过的:

echo base64_decode($string);

结果:

SSBuZWVkIHlvdXIgaGVscA = ខ្លាំង​ណាស់​

上面的结果不是我需要的,下面是我需要的:

I need your help ខ្លាំង​ណាស់​

所以,谁能告诉我如何使用 php 做到这一点。

这应该适合您:

<?php
    $str = "I need your help 4Z6B4Z+S4Z6b4Z624Z+G4Z6E4oCL4Z6O4Z624Z6f4Z+L4oCL";

    for($count = 0; $count < strlen($str); $count++) {
        $temp = substr($str, $count, strlen($str));
        if ( base64_encode(base64_decode($temp, true)) === $temp) {
            echo substr($str, 0, $count) . base64_decode($str);
            break;
        }
    }
?>