SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    encoded data and spaces

    Hai folks,

    My sms provider require all unicode sms sent to be formated to USC-2 Encoding.

    Text to be sent : درويش
    UCS-2 Encoded : 0 b a 4 0 b b f 0 b b 0 0 b c

    function used for the conversion :

    Code:
    <?php
    function convert($str){
    	$str = mb_convert_encoding(bin2hex(mb_convert_encoding($str, 'UCS-2', 'auto')), 'UCS-2', 'auto');
    	return $str;
    }
    ?>
    Now, you can see converted arabic text in ucs-2 format and you see every character is proceeding a space.

    Strange, i could not remove those spaces using the str_replace. Because the sms api require those spaces removed.

    PHP Code:
    <?php
    function convert($str){
        
    $str mb_convert_encoding(bin2hex(mb_convert_encoding($str'UCS-2''auto')), 'UCS-2''auto');
        return 
    str_replace(' ','',$str);
    }
    ?>
    What could be the problem

    * when i echo the text,the spaces are not showing in browser. but i want to fopen in my case.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,245
    Mentioned
    156 Post(s)
    Tagged
    0 Thread(s)
    Then the spaces are not truly spaces, they are likely considered "low" values (meaning before the space on the ASCII table/your encoding table).

    You may want to try this (although it may still not work, because the value may not match)
    PHP Code:
    $str preg_replace('/[\s]/'''$str); //replaces whitespace, form feeds, tabs, carriage returns and new lines 
    Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number
    PHP Code:
    $str preg_replace('/[^a-z0-9]+/i'''$str); 

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Then the spaces are not truly spaces, they are likely considered "low" values (meaning before the space on the ASCII table/your encoding table).

    You may want to try this (although it may still not work, because the value may not match)
    PHP Code:
    $str preg_replace('/[\s]/'''$str); //replaces whitespace, form feeds, tabs, carriage returns and new lines 
    Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number
    PHP Code:
    $str preg_replace('/[^a-z0-9]+/i'''$str); 
    Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number
    PHP Code:
    $str preg_replace('/[^a-z0-9]+/i'''$str); 
    Workes charm!!!!
    Thank you cpradio


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •