Why is it impossible to change all the characters from a full-width character to a half-width character?

I want to change all the characters from full-width characters to half-width characters, write the following codes to complete the task. For example, to change all characters of the full width to

Codebit.cn - 聚合小段精华代码 

in half-width characters, in

  codebit.cn - 聚合小段精华代码 

There are two ways to achieve the goal, but they all failed.
The entire php file was 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:

enter image description here

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:

enter image description here

How to fix two of them?

I solved the problem with method 1, the fixed codes are as follows:

 <?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 - 聚合小段精华代码"); ?> 
+8
php
source share
6 answers

Method 2:

I changed preg_replace to preg_replace_callback and used an anonymous function to disable the warning. Your regular expression was wrong, I changed it. Simple search in anonymous function.

 <?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_callback( "/([\xEF][\x80-\xBF]+)/", function($m) use($Queue) { if (array_key_exists($m[0], $Queue)) { return $Queue[$m[0]]; } else { return $m[0]; } }, $Str); } $str = "Codebit.cn - 聚合小段精华代码"; echo $str; echo "<br />"; echo fulltohalf($str); 
0
source share

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 save the case of letters as a bonus.

Link: http://php.net/mb_convert_kana

+7
source share

strstr () looks for a string in another string.

You call it with a string and an array.

Why not just return the appropriate 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 may be inefficient to build an array with every function call.

You might want to define it in your main function and just pass it to the fulltohalf () function or not worry about the function at all.

As always in these cases, I will add that you should avoid using mysql in favor of mysqli (improved).

ADDITIONAL

If converting from one character set to another, I will use php functions and not reinvent the wheel.

see mb_convert_encoding and supported encodings

I cannot be sure which encodings you are going to use, but something like the following might do the trick:

 mb_convert_encoding($str, "UTF-8","UCS-2") 
+1
source share

A simple solution would be to use str_replace() with string arrays, effectively replacing multibyte ones. For example:

 $str = 'Fooooo'; echo str_replace(array('F', 'o'), array('F', 'o'), $str); 

Probably a more robust solution would use mb_convert_kana() , for example:

 $str = 'Fooooo'; echo mb_convert_kana($str, 'a', 'UTF-8'); // change UTF-8 to fit your input charset 

Both solutions will produce:

 Fooooo 
+1
source share

Instead of using preg_replace I think that in this case it is better to use str_replace . We will look for a string for the keys of the queue array and replace them with the corresponding values. It is also a very readable and easy solution.

 function fulltohalf($str) { $queue = [ '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 str_replace(array_keys($queue),array_values($queue),$str); } 

Note that I had a page in UTF-8 mode by declaring:

 <meta charset="UTF-8"> 

Here is my test:

 $str = "Codebit.cn - 聚合小段精华代码"; echo $str; echo "<br>"; echo fulltohalf($str); 

Here are my results:

Codebit.cn - 聚合 小段 精华 代码
Codebit.cn - 聚合 小段 精华 代码

+1
source share

I will just fix your mistake.

 <?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 = ''; for($i = 0; $i < strlen($str); $i++){ $char = $str[$i]; $new .= (isset($arr[$char]))? $arr[$char]: $char; } return $new; } $str="Codebit.cn - 聚合小段精华代码"; echo fulltohalf($str); ?> 

Firstly, your code is used by foreach, and I changed to ($ i = ...) because the line does not support the foreach syntax (iteration). Secondly, your if / else phrase is incompatible with logic.

 if()$arr[$char]; else $arr; // <- else array!? 

and I recorded that

 if() $converted_char; else $original_char; 

Alternatively, you can use a different method.

everyone says: mb_convert_kana (). Yes, this is a great solution.

0
source share

All Articles