Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP检查字符串是否包含一定百分比的字母数字字符_Php_Alphanumeric_Non Alphanumeric - Fatal编程技术网

PHP检查字符串是否包含一定百分比的字母数字字符

PHP检查字符串是否包含一定百分比的字母数字字符,php,alphanumeric,non-alphanumeric,Php,Alphanumeric,Non Alphanumeric,我有一个php字符串$comment有时$comment框会包含一些非字母数字字符,有没有办法找出$comment中字母数字的百分比 谢谢你可以试试这样的方法:(不是特别有效) 但是Regex可能是一种更简单的方法,更简单我的第一个想法是使用Regex剥离非字母字符,然后将字符串长度与原始字符串进行比较。数一数。除以字符总数。乘以100。我很好奇:在这个简单的过程中,哪两个步骤你自己做起来有困难? $comment_alpha = preg_replace('/[^a-z\d]+/i', '

我有一个php字符串$comment有时$comment框会包含一些非字母数字字符,有没有办法找出$comment中字母数字的百分比


谢谢

你可以试试这样的方法:(不是特别有效)



但是Regex可能是一种更简单的方法,更简单

我的第一个想法是使用Regex剥离非字母字符,然后将字符串长度与原始字符串进行比较。数一数。除以字符总数。乘以100。我很好奇:在这个简单的过程中,哪两个步骤你自己做起来有困难?
$comment_alpha = preg_replace('/[^a-z\d]+/i', '', $comment);
$alpha_percent = 100 * strlen($comment_alpha) / strlen($comment);
<?php
$string = "TestItOut##@22383";



$all = array(
"0","1","2","3","4","5","6","7","8","9",          
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",     
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
);

$num_alphanum = 0;
foreach ( $all as $char ) {
    $num_alphanum += substr_count( $string, $char );
}

$percent = ( $num_alphanum / strlen( $string ) ) * 100;

echo $percent . "%";

?>