Search code examples
phpsubstr

PHP - Get Chinese string between two tags


Here is the code I'm using:

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = mb_strpos($string, $start);
    if ($ini == 0) return '';
    $ini += mb_strlen($start);
    $len = mb_strpos($string, $end, $ini) - $ini;
    return mb_substr($string, $ini, $len, mb_detect_encoding($string));
}

$string = '<div>財物類</div>';

$parsed = get_string_between($string, '<div>', '</div>');

echo $parsed;

Here is the result I'm getting: 財物類

The result I obviously want is: 財物類


Solution

  • It works fine in my env, you simply need to set the file encoding to UTF-8 using your text editor. Or if it is a Browser problem, to be sure, add

    header('Content-Type: text/html; charset=utf-8');
    

    before

    echo $parsed;
    

    Which would force the browser to interpret the server response encoding as UTF-8, it is always advised to send this header actually if you are coding in UTF-8 (your response e.g. HTML encoding is UTF-8).