I have a string in UTF8 encoding that has many shift+space char between characters, I wanna replace them with space! How it can be done ?
1 に答える
The simplest way is to just use a half space into a regexp:
$new = preg_replace("/ /", "-SPACE-", $yourstring);
provided that your source file is UTF8 coded (so that the thing between slashes in the regexp is actually a UTF8 halfspace), and your PHP has full UTF8 support (I believe all installations do since sometime after 5.0, but you never know...).
If you can't do that and need the UTF8 hex representation, you can do a hex dump of the file (or the shortest file you can with the character in it) and figure out what the codes are. You will see something like C3 A0 and you'll know that you can use /...\xc3\xa0.../
as a regexp; remember that the backslash might need to be escaped.
Otherwise, you can maybe do something like this - I use another UTF8 character because I have no idea of what your editor uses as a "half-space" - you just prepare a string with that one character and decode it in PHP.
<?php
$string = "é"; // THE SOURCE FILE MUST BE UTF8 CODED, OF COURSE...
$hex = bin2hex($string);
$seq = str_split($hex, 2);
$search = "\\x".implode("\\x", $seq);
print "The sequence is $search .\n";
?>
Now if you wanted to use it into a replacement regexp, you just plug it in:
print preg_replace("/$search/", "(E)", "Déja vu");
outputs "D(E)ja vu" on my system.
NOTE: are you sure you need a regexp for that? str_replace
might be faster and not require hex codes at all:
$new = str_replace(" ", " ", $old); // The first space is really a "half space"
UPDATE: you can do the above directly in MySQL too (make a backup of the DB first ;-) ):
UPDATE table SET field = REPLACE(field, '<A HALF SPACE HERE>', ' ');
(You might need to issue a SET NAMES UTF8;
first, to be certain of the library character encoding).