How to replace ideographic space in a PHP string?

ideographic space http://www.charbase.com/3000-unicode-ideographic-space , this is CJK punctuation. It looks like normal space, but actually it occupies 2 positions on the screen instead of 1 (for example, a Chinese character)

I tried using str_replace(" ","",$mystring) to get rid of them, but of course this will not work because the space I entered here is ASCII space. I also tried to manually enter the ideographic space using the Chinese character input method, but it looks like I will also get rid of the code for other characters and it will return gibberish.

So how can I get rid of these spaces?

+5
source share
3 answers

I was able to replace the symbol just fine by copying the symbol from the information page that you linked to. You might want to create a CONST alias for the ideographic space to help make and find / replace coding more understandable.

 // contains ideographic space between words $start = 'before after'; // contains ideographic space in needle parameter $test1 = str_replace(' ', '_', $start); // contains ideographic space define('ID_SPACE', ' '); $test2 = str_replace(ID_SPACE, '&', $start); // contains normal space in needle parameter $test3 = str_replace(' ','_',$start); // make sure we are using utf8 for this test header('Content-Type: text/html; charset=utf-8'); echo $start.'<br/>'; echo $test1.'<br/>'; echo $test2.'<br/>'; echo $test3; 

output:

 before after before_after before&after before after 

Edit in response to a question

Until you see it, the character is displayed in the window shown, just drag and drop to select, like any other text, and then you can paste it as needed. You can also just copy the code from my answer, which contains a space. If you see something like   , then you need to set your encoding to utf-8

enter image description here

+2
source

You can directly convert objects from their escaped numeric values. For many years I had the following function. I did not write this, and I am afraid that I do not remember where I found it. It's a little hack, but damn useful, I think.

 <?php function code2utf($num) { if($num<128)return chr($num); if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128); if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128); if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128); return ''; } print "a" . code2utf(0x3000) . "b" . code2utf(0x1f44d) . "\n"; 

And when I run this, I see:

 $ php -f utftest a bπŸ‘ 

Note that what looks like two spaces is one double-wide character.

Perhaps you can use the above function to create your input string, for example:

 str_replace(code2utf(0x3000),"",$mystring); 

The obvious advantage of such a solution as the WebChemist copy and paste solution is that it is fully programmatic and does not require any special functions as part of the programmer's tools. You will not accidentally overwrite the ID_SPACE character when reformatting your code, and the function can be reused for other UTF8 characters that you may need, without having to have these characters inside your code.


Of course, otherwise you can do it with the PHP built-in html_entity_decode() function. The following are results identical to my function using HTML escaped characters as input:

 $ php -r 'print html_entity_decode("a&#x3000;b&#x1f44d;") . "\n";' a bπŸ‘ 
0
source

The method that also worked for me, raw-coding it in HTML entities and str_replace back to normal space.

 //The space we're looking out for $ideoSpace = "%26%23x3000%3B"; $space = "%20"; //Search string (Notice the wider space) $searchstr = "Please find me a Oil Filter"; //Begin conversion $searchstr = rawurldecode( str_replace( $ideoSpace, $space, rawurlencode( $searchstr ))); //echos "Please find me a Oil Filter" 

This may not be the most elegant solution. However, unfortunately, the search did not work for us, since implode() could not split the strings for our Japanese clients.

0
source

Source: https://habr.com/ru/post/1238211/


All Articles