欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 正则匹配 求解答

程序员文章站 2024-01-28 15:12:04
...

这是网上找的一段全角符号转半角符号的函数,使用了下确实有效,使用的原理是中文标点的第二个字节减去0X80(即128)所得的数字就是半角所得的数字,但还是不符合我的需求,我希望的能够从一段文本中将全角符号转成半角符号而对文字不转换。

例如:

原文:如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义。
转为:如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义.

请问这个该如何匹配???

还是需要说将文本中的字符串转成数组,例遍数组遇到全角转成半角,再组成文本。

回复内容:

这是网上找的一段全角符号转半角符号的函数,使用了下确实有效,使用的原理是中文标点的第二个字节减去0X80(即128)所得的数字就是半角所得的数字,但还是不符合我的需求,我希望的能够从一段文本中将全角符号转成半角符号而对文字不转换。

例如:

原文:如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义。
转为:如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义.

请问这个该如何匹配???

还是需要说将文本中的字符串转成数组,例遍数组遇到全角转成半角,再组成文本。

你的方法不错,不过复杂了点,看我的

$arr1 = ["1","2","3","A","B","C","D","F","W","S","\",""",",",".","?","<",">","{","}","[","]","*","&","^","%","#","@","!","~","(",")","+","-","|",":",";","。"];
$arr2=["1","2","3","A","B","C","D","F","W","S","\\","\"",",",".","?","","{","}","[","]","*","&","^","%","#","@","!","~","(",")","+","-","|",":",";","."];
$str="如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义。";
echo $str."
"; for ($i=0;$i

运行结果

如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义。
如果我连身边最重要的人都保护不了的话,我当这个魔君还有什么意义.
相关标签: php