如何使用PHP计算字符串中的特殊字符

如何使用PHP计算字符串中的特殊字符,php,regex,string,function,Php,Regex,String,Function,我需要知道如何使用PHP查找字符串中特殊字符的出现次数 这是代码 <?php $message=$_POST['Message']; //$rep=preg_replace("@ # $ % ^ & / . * @"," ",$message); //$rep=preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $message); $rep = preg_replace("/[^a-zA-Z]+/", "", $mes

我需要知道如何使用PHP查找字符串中特殊字符的出现次数

这是代码

<?php

$message=$_POST['Message'];
//$rep=preg_replace("@ # $ % ^ & / . * @"," ",$message);
//$rep=preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $message);
$rep = preg_replace("/[^a-zA-Z]+/", "", $message);
echo "There are ".str_word_count($message)." words found in the given message"."<br/>";

$len=strlen($rep);
echo "length of the message is ".$len;
$delims="?#";
$word = strtok($message, $delims);

echo "delim ".$word."<br/>";
echo $delimlen=strlen($word)."<br/>";

echo "without special ".$rep;


?>

使用此正则表达式,看看是否有帮助

$pattern = '/[!@#$%^&*()]/'  // will match one occurrence of any symbol inside the []

对字符串执行全局正则表达式匹配。搜索模式中给定的正则表达式的所有匹配项,并按标志指定的顺序将它们放入匹配项中


在找到第一个匹配项后,将从最后一个匹配项的末尾继续后续搜索。

使用此正则表达式,看看这是否有帮助

$pattern = '/[!@#$%^&*()]/'  // will match one occurrence of any symbol inside the []

对字符串执行全局正则表达式匹配。搜索模式中给定的正则表达式的所有匹配项,并按标志指定的顺序将它们放入匹配项中


在找到第一个匹配项后,后续搜索将从最后一个匹配项结束时继续进行。

如果要获取替换零件的数量,则需要为预匹配调用提供两个以上参数,如下所示:

$replaceCount = 0;
preg_replace("/[^a-zA-Z]+/", "", $message, -1, $replaceCount);

如果您想获得更换零件的数量,您需要为preg_match调用另外两个参数,如下所示:

$replaceCount = 0;
preg_replace("/[^a-zA-Z]+/", "", $message, -1, $replaceCount);

它没有显示特殊字符。这是你要找的吗??和
preg_match_all(“/([^\dA-z])/”,$string,$matches)它没有显示特殊字符。这是你要找的吗??和
preg_match_all(“/([^\dA-z])/”,$string,$matches)