Saturday, August 3, 2019

WT 9 - PHP - STATES SEARCH OPERATIONS

09. WRITE A PHP PROGRAM NAMED STATES.PY THAT DECLARES A VARIABLE STATES WITH VALUE "MISSISSIPPI ALABAMA TEXAS MASSACHUSETTS KANSAS". WRITE A PHP PROGRAM THAT DOES THE FOLLOWING:

A. SEARCH FOR A WORD IN VARIABLE STATES THAT ENDS IN XAS. STORE THIS WORD IN ELEMENT 0 OF A LIST NAMED STATESLIST.

B. SEARCH FOR A WORD IN STATES THAT BEGINS WITH K AND ENDS IN S. PERFORM A CASEINSENSITIVE COMPARISON. [NOTE: PASSING RE.IAS A SECOND PARAMETER TO METHOD COMPILE PERFORMS A CASE-INSENSITIVE COMPARISON.] STORE THIS WORD IN ELEMENT1 OF STATESLIST.

C. SEARCH FOR A WORD IN STATES THAT BEGINS WITH M AND ENDS IN S. STORE THIS WORD IN ELEMENT 2 OF THE LIST.

D. SEARCH FOR A WORD IN STATES THAT ENDS IN A. STORE THIS WORD IN ELEMENT 3 OF THE LIST.

 SOLUTION  1 

1. If xampp is in the system, save the program in C:\xampp\htdocs (start apache in xampp control panel)

2. If no xampp in the system, save the program in C:\Program Files\Apache Group\Apache2\htdocs   & Open Google Chrome & type http://localhost/prog9.php

prog9.php

<html>
<body>
<?php 
$states = "Mississippi Alabama Texas Massachusetts Kansas";
$b = explode(' ',$states);
echo "<br>ORIGINAL ARRAY :<br>"; 
foreach ( $b as $i => $value )
echo "states[$i] = $value<br>";
foreach ($b as $c) 
{
$n = strlen($c);
if($c[$n-1]=='s' && $c[$n-2]=='a' && $c[$n-3]=='x') $d[0] = $c;
if($c[0]=='K' && $c[$n-1]=='s') $d[1] = $c;
if($c[0]=='M' && $c[$n-1]=='s') $d[2] = $c;
if($c[$n-1]=='a') $d[3] = $c;
}
echo "<br>RESULTANT ARRAY :<br>"; 
for ($i=0; $i < count($d); $i++)
echo "statesList[$i] = $d[$i]<br>";
?>
</body>
</html>

STEPS :( click on image to zoom )





OUTPUT :( click on image to zoom )



 SOLUTION  2 

prog9.php

<?php
$states = "Mississippi Alabama Texas
Massachusetts Kansas"; $statesArray = [];
$states1 = explode(' ',$states);
echo "Original Array :<br>";
foreach ( $states1 as $i => $value )
print("STATES[$i]=$value<br>");
foreach($states1 as $state) {
if(preg_match( '/xas$/', ($state)))
$statesArray[0] = ($state);
}
foreach($states1 as $state) {
if(preg_match('/^k.*s$/i', ($state)))
$statesArray[1] = ($state);
}
foreach($states1 as $state) {
if(preg_match('/^M.*s$/', ($state)))
$statesArray[2] = ($state);
}
foreach($states1 as $state){
if(preg_match('/a$/', ($state)))
$statesArray[3] = ($state);
}
echo "<br><br>Resultant Array :<br>";
foreach ( $statesArray as $array => $value )
print("STATES[$array]=$value<br>");
?>

OUTPUT :( click on image to zoom )


1 comment:

  1. PHP is what brought me into the world of web design development in the first place.
    Ruby and Python have not yet been attempted. But, because of my love for PHP, I'll always utilize it.

    ReplyDelete