php如何實現全角符號轉半角

藏色散人
發布: 2023-03-08 18:14:02
原創
2814 人瀏覽過

php实现全角符号转半角的方法:首先创建一个PHP示例文件;然后定义一个“SBC_DBC($str,$args2=1) {...}”方法;最后通过“str_replace($DBC,$SBC,$str);”方式实现全角转半角即可。

php如何實現全角符號轉半角

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

PHP 全角和半角转换函数

代码如下:

<?PHP
function SBC_DBC($str,$args2=1) { //半角和全角转换函数,第二个参数如果是0,则是半角到全角;如果是1,则是全角到半角
$DBC = Array(
&#39;0&#39; , &#39;1&#39; , &#39;2&#39; , &#39;3&#39; , &#39;4&#39; , 
&#39;5&#39; , &#39;6&#39; , &#39;7&#39; , &#39;8&#39; , &#39;9&#39; ,
&#39;A&#39; , &#39;B&#39; , &#39;C&#39; , &#39;D&#39; , &#39;E&#39; , 
&#39;F&#39; , &#39;G&#39; , &#39;H&#39; , &#39;I&#39; , &#39;J&#39; ,
&#39;K&#39; , &#39;L&#39; , &#39;M&#39; , &#39;N&#39; , &#39;O&#39; , 
&#39;P&#39; , &#39;Q&#39; , &#39;R&#39; , &#39;S&#39; , &#39;T&#39; ,
&#39;U&#39; , &#39;V&#39; , &#39;W&#39; , &#39;X&#39; , &#39;Y&#39; , 
&#39;Z&#39; , &#39;a&#39; , &#39;b&#39; , &#39;c&#39; , &#39;d&#39; ,
&#39;e&#39; , &#39;f&#39; , &#39;g&#39; , &#39;h&#39; , &#39;i&#39; , 
&#39;j&#39; , &#39;k&#39; , &#39;l&#39; , &#39;m&#39; , &#39;n&#39; ,
&#39;o&#39; , &#39;p&#39; , &#39;q&#39; , &#39;r&#39; , &#39;s&#39; , 
&#39;t&#39; , &#39;u&#39; , &#39;v&#39; , &#39;w&#39; , &#39;x&#39; ,
&#39;y&#39; , &#39;z&#39; , &#39;-&#39; , &#39; &#39;  , &#39;:&#39; ,
&#39;.&#39; , &#39;,&#39; , &#39;/&#39; , &#39;%&#39; , &#39;#&#39; ,
&#39;!&#39; , &#39;@&#39; , &#39;&&#39; , &#39;(&#39; , &#39;)&#39; ,
&#39;<&#39; , &#39;>&#39; , &#39;"&#39; , &#39;'&#39; , &#39;?&#39; ,
&#39;[&#39; , &#39;]&#39; , &#39;{&#39; , &#39;}&#39; , &#39;\&#39; ,
&#39;|&#39; , &#39;+&#39; , &#39;=&#39; , &#39;_&#39; , &#39;^&#39; ,
&#39;¥&#39; , &#39; ̄&#39; , &#39;`&#39;
);
$SBC = Array( //半角
&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, 
&#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;,
&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, 
&#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;,
&#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, 
&#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;,
&#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, 
&#39;Z&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;,
&#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, 
&#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;,
&#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, 
&#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;,
&#39;y&#39;, &#39;z&#39;, &#39;-&#39;, &#39; &#39;, &#39;:&#39;,
&#39;.&#39;, &#39;,&#39;, &#39;/&#39;, &#39;%&#39;, &#39;#&#39;,
&#39;!&#39;, &#39;@&#39;, &#39;&&#39;, &#39;(&#39;, &#39;)&#39;,
&#39;<&#39;, &#39;>&#39;, &#39;"&#39;, &#39;\&#39;&#39;,&#39;?&#39;,
&#39;[&#39;, &#39;]&#39;, &#39;{&#39;, &#39;}&#39;, &#39;\\&#39;,
&#39;|&#39;, &#39;+&#39;, &#39;=&#39;, &#39;_&#39;, &#39;^&#39;,
&#39;$&#39;, &#39;~&#39;, &#39;`&#39;
);
if($args2==0)
return str_replace($SBC,$DBC,$str);  //半角到全角
if($args2==1)
return str_replace($DBC,$SBC,$str);  //全角到半角
else
return false;
}
/*
$str = "alskdf";
echo $str;
echo "<br>";
echo SBC_DBC($str,0);
echo SBC_DBC($str,1);
*/
?>
登入後複製

推荐学习:《PHP视频教程

以上是php如何實現全角符號轉半角的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!