Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ok so i have array for example $arr= "/43sdsd555ksldk66sd"544fdfd";

I take numbers using preg_match_all '/\d+/', and array_map('intval', $zni[0]);

Now the problem is i need to reverse those whole int to see if they are symmetric,like 555 and 66 , and if they are GET A TOTAL OF THEM.(total of only symmetric numbers)

i tried to use function " strrev "and got symmetric numbers, but i don't know how to put them in a one place IF THEY ARE 'TRUE' and calculate them.

What I have tried:

<form class= "sredina" action="" method="get" class="form-signin">       
<input type="text" name="niz" id="niz" placeholder="unesi"  />
<input type="submit" name="submit"  >
</form>  

<?php


$numbers = "";

if (isset($_GET['submit'])) {
	 
	$numbers = ($_GET['niz']);
	preg_match_all('/\d+/', $numbers, $zni);
	$numtwo= array_map('intval', $zni[0]);
     
   
	 
}

foreach ($numtwo as $num) {
 

$reverse = strrev($num); 
var_dump($reverse);
 
if ($num == $reverse) {
	 
	$reverse = "true";
 
}
else {
	 
	$reverse = "false";
	
}

var_dump($reverse);
 
}
Posted
Updated 3-Apr-18 3:20am
Comments
Patrice T 2-Apr-18 21:09pm    
When you have "44455", does it count as "444" and "55" ?

1 solution

actually there is a much simplier solution to do- 
<?php






$input = "/43sdsd555ksldk66sd\"544fdfd";
$total = 0;

preg_match_all('/\d+/', $input, $m);
foreach ($m[0] as $d)
    if ($d == strrev($d))
        $total += $d;

var_dump($total); // => int(621)
echo $total;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900