我有rand功能,如;
$mynumbers = rand(1111,9999);
echo $mynumbers;
示例输出是 3582
我有另一个字符串
$mystring = "Ja9Js78I4PhXiF464R6s7ov8IUF"; (Have 1 number, must be turn 1 (only 8 have))
$mystring2 = "Ja3Js73I4P1X5iF564R8s2ov8IUF"; (Have 4 numbers, must be turn 4 (have all of them))
我想用功能知道这个;
$ mystring有多少个数字?里面$ mynumbers 多少次?通过这个过程?我该怎么办?
答案 0 :(得分:1)
根据您的上次评论。将整数视为一个字符串(PHP很擅长)。并按字符迭代。
<?php
$foo = '1234';
$mystring = [];
$mystring[] = 'A1B2KLDLDF3'; //3
$mystring[] = 'XXXX4XXXX'; //1
foreach ($mystring as $key => $string) {
echo "mystring {$key}: ";
$c = 0;
foreach(str_split($foo) as $char) {
$c = $c + substr_count($string, $char);
}
echo $c . '<br/>';
}
mystring 0:3
mystring 1:1
由于这是PHP,您还需要了解mb_
多字节函数。请参阅:http://php.net/manual/en/function.mb-substr-count.php
<强>更新强>
听起来你应该清理你正在检查的字符串然后如果要丢弃所有重复项......当然可以使用substr
或其他方法,或许比substr_count
更高效。< / p>
$mystring = 'A111111B2KLDLDF333'; //3
$mystring = implode('',array_unique(str_split($mystring)));
//gives 'A1B2KLDF3'