Php 在字符串中查找素数和复合数

Php 在字符串中查找素数和复合数,php,Php,我有以下随机生成的字符串: $text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m'; 我需要的是从中取出所有整数,将它们分类为素数或复合数,并估计它们的和。所有数字都应假定为一位数字,因此这会将每组的值缩短为四位: $primes = array(2, 3, 5, 7); $composites = array(4, 6, 8, 9); 这意味着:素数:5,3,3,2=13和复合数:4,8,8,9,8=37,因为重复的数字也算在内 我试着像这样抓住数字

我有以下随机生成的字符串:

$text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m';
我需要的是从中取出所有整数,将它们分类为素数或复合数,并估计它们的和。所有数字都应假定为一位数字,因此这会将每组的值缩短为四位:

$primes     = array(2, 3, 5, 7);
$composites = array(4, 6, 8, 9);
这意味着:
素数:5,3,3,2=13
复合数:4,8,8,9,8=37
,因为重复的数字也算在内

我试着像这样抓住数字:

$asArray = str_split($text);

foreach ($asArray as $element) {
    if (is_int($element)) {
        echo $element;
    }
}

但它似乎在一个空白页结束。所以我的问题是如何找出字符串中的数字,然后将它们分类为素数或复合数?

这里有素数和复合数之和:

$text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m';
$primes     = array(2, 3, 5, 7);

$sum_primes = $sum_composites = 0; 

preg_match_all("/\d/", $text, $matches);

foreach($matches[0] as $number)
{
    if (in_array($number, $primes))
        $sum_primes += $number; 
    else
        $sum_composites += $number; 
} 

echo "Sum of primes: ".$sum_primes."\n";
echo "Sum of composites: ".$sum_composites."\n";
它会打印

Sum of primes: 20
Sum of composites: 38

这里有素数和复合数之和:

$text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m';
$primes     = array(2, 3, 5, 7);

$sum_primes = $sum_composites = 0; 

preg_match_all("/\d/", $text, $matches);

foreach($matches[0] as $number)
{
    if (in_array($number, $primes))
        $sum_primes += $number; 
    else
        $sum_composites += $number; 
} 

echo "Sum of primes: ".$sum_primes."\n";
echo "Sum of composites: ".$sum_composites."\n";
它会打印

Sum of primes: 20
Sum of composites: 38

这里有素数和复合数之和:

$text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m';
$primes     = array(2, 3, 5, 7);

$sum_primes = $sum_composites = 0; 

preg_match_all("/\d/", $text, $matches);

foreach($matches[0] as $number)
{
    if (in_array($number, $primes))
        $sum_primes += $number; 
    else
        $sum_composites += $number; 
} 

echo "Sum of primes: ".$sum_primes."\n";
echo "Sum of composites: ".$sum_composites."\n";
它会打印

Sum of primes: 20
Sum of composites: 38

这里有素数和复合数之和:

$text = 's$bp4q1hsq3@g88nsjm5hr#i9#3078e2m';
$primes     = array(2, 3, 5, 7);

$sum_primes = $sum_composites = 0; 

preg_match_all("/\d/", $text, $matches);

foreach($matches[0] as $number)
{
    if (in_array($number, $primes))
        $sum_primes += $number; 
    else
        $sum_composites += $number; 
} 

echo "Sum of primes: ".$sum_primes."\n";
echo "Sum of composites: ".$sum_composites."\n";
它会打印

Sum of primes: 20
Sum of composites: 38

去读一下
is\u int
做了什么。没错,因为我误用了一个函数而否决它。去读一下
is\u int
做了什么。没错,因为我误用了一个函数而否决它。去读一下
is\u int
做了什么。没错,向下投票是因为我误用了一个函数。去读一下
is\u int
的作用。没错,向下投票是因为我误用了一个函数。是的,我刚刚用
is\u numeric()
和一些
if()
实现了这一点。我现在需要想一想如何逐一总结。谢谢你的回答,我很感谢你的努力:)好了@JessieStalk:)是的,我刚刚用
is\u numeric()
和一些
if()
实现了这一点。我现在需要想一想如何逐一总结。谢谢你的回答,我很感谢你的努力:)好了@JessieStalk:)是的,我刚刚用
is\u numeric()
和一些
if()
实现了这一点。我现在需要想一想如何逐一总结。谢谢你的回答,我很感谢你的努力:)好了@JessieStalk:)是的,我刚刚用
is\u numeric()
和一些
if()
实现了这一点。我现在需要想一想如何逐一总结。谢谢你的回答,tho,我感谢你的努力:)好了@JessieStalk:)