Search code examples
phpexpressionnon-ascii-characters

PHP: Can I remove ‪ #‎ from string


I need to remove ‪#‎ from string. I found this method:

$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string); 

It doesn't work for the Thai language. I want to remove like this:

from

‪#‎Apple‬ ‪#‎ผลไม้‬

to

#Apple #ผลไม้

Solution

  • I can not understand why str_replace() did not work for you. This will do the job:

    function cleanString($string) {
        $search = array('‪', '‎', '‬');
        $replace = array('', '', '');
    
        return str_replace($search, $replace, $string);
    }
    $string = '‪#‎Apple‬ ‪#‎ผลไม้‬';
    
    echo $string . "\n";
    echo cleanString($string) . "\n";
    

    Output is:

    ‪#‎Apple‬ ‪#‎ผลไม้‬
    #Apple #ผลไม้
    

    Working example can be found at http://sandbox.onlinephpfunctions.com/code/bbdbdf0758e5ea06faf32281021ae859b6d75a51