I want to change all the characters from full width characters into half width characters, write the following codes to do the job. For example, to change all full width characters in
Codebit.cn - 聚合小段精华代码
into half width characters ,into
codebit.cn - 聚合小段精华代码
There are two methods to acheive the goal,but all of them failed.
All the php file were saved as utf-8 format.
method 1:
<?php
function fulltohalf($str){
$arr=Array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z', '-' => '-'
);
$new = '';
foreach ($str as $char){
if (isset ($arr[$char]))
$new .= $arr[$char];
else
$new .= $arr;
}
return $new;
}
$str="Codebit.cn - 聚合小段精华代码";
echo fulltohalf($str);
?>
Error message:
method 2:
<?php
function fulltohalf($Str) {
$Queue = Array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z', '-' => '-'
);
return preg_replace("/([\xA3][\xB0-\xB9\xC1-\xDA\xE1-\xFA])/e","\$Queue[\\1]", $Str);
}
$str = "Codebit.cn - 聚合小段精华代码";
echo $str;
echo "<br />";
echo fulltohalf($str);
?>
Error message:
How to fix two of them?
I solved the method 1 problem ,the fixed codes are as the following.
<?php
function fulltohalf($str){
$arr=Array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z', '-' => '-'
);
$new = '';
preg_match_all('/./u', $str, $results);
$str=$results[0];
foreach ($str as $char){
if (isset ($arr[$char]))
$new .= $arr[$char];
else
$new .= $char;
}
return $new;
}
echo fulltohalf("Codebit.cn - 聚合小段精华代码");
?>
For Windows: Use F10 within an online form to toggle quickly between full-width and half-width characters.
In JIS X 0208, katakana, hiragana, and kanji are all encoded (and displayed as full-width characters; there are no half-width characters), though the ordering of the kana is different – see JIS X 0208#Hiragana and katakana.
Enter the alphanumeric text, and then press the F9 key to convert to Full-width Alphanumeric characters or the F10 key to convert to Half-width Alphanumeric characters.
Half-width refers to characters where the horizontal and vertical length ratio is 1:2. These characters are horizontally narrow. English letters, numbers, spaces, and punctuation marks such as comma and period are half-width by default.
One line of code:
$str="Codebit.cn - 聚合小段精华代码";
$str = mb_convert_kana($str, "rnaskhc", 'UTF-8');
echo $str;
and
Codebit.cn - 聚合小段精华代码
becomes
Codebit.cn - 聚合小段精华代码
and you can keep the case of the letters as a bonus.
Ref: http://php.net/mb_convert_kana
strstr() searches for an occurrence of a string within another string.
You are calling it with a string and an array.
why not just return the corresponding values for the keys?
function fulltohalf($str){
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3',
'4' => '4','5' => '5', '6' => '6', '7' => '7',
'8' => '8', '9' => '9','A' => 'A',
....
);
$new = ''; //initialise output
$old = str_split($str); // convert string to array of characters
foreach ($old as $char){
if (isset $arr[$char]) //check if the input is a key in the array
$new .= $arr[$char]; //add to output
else
$new .= '_'; //put something to indicate missing/undefined
}
return $new;
}
It might be inefficient to construct the array on every function call.
You might want to define it in your main function and just pass it to your fulltohalf() function, or not bother with a function at all.
As always in these cases, I will add that you should avoid using mysql in favour of mysqli (improved).
ADDITIONAL
If converting from one character set to another, I would use the php functions, rather than reinventing the wheel.
see mb_convert_encoding and supported encodings
I can't be sure which encodings you are going from/to, but something like the following might do the trick:
mb_convert_encoding($str, "UTF-8","UCS-2")
A simple solution would be using str_replace()
with arrays of strings, effectively doing a multibyte replace. For example:
$str = 'Fooooo';
echo str_replace(array('F', 'o'), array('F', 'o'), $str);
A probably more robust solution would be using mb_convert_kana()
, like so:
$str = 'Fooooo';
echo mb_convert_kana($str, 'a', 'UTF-8'); // change UTF-8 to fit your input charset
Both solutions will output:
Fooooo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With