imported_nobody
New member
I'm working on PHP homework and I can't seem to get the search functions to work correctly. The first two work alright, but I don't think they're going into the elements correctly and the ones that are noted in the comments aren't working at all and I get offset errors.
I've gotta do five different searches. One that searches $states that ends in xas and stores it in element0. A second that searches $states that begins with k and ends in s, stored in element 1. A third search that begins with M and ends in s, stored in element2. A fourth search that ends in a, stored in element3. Finally, a fifth that searches the beginning of the string that starts with M.
I've worked out all the code the best I could, but only the ones not in comments work and I'm not sure exactly why the others are giving errors and why they're not being stored properly.
<?php
$states = "Mississippi Alabama Texas Massachusetts Kansas";
print("This script will search, '$states' <br /> <br />");
//Search for any words ending in 'xas'
if (ereg( "[[:<:]]([a-zA-Z]*xas)[[:>:]]", $states, $statesArray ) )
print( "Match found ending in 'xas': " .
$statesArray[ 0 ] ."<br />");
//Search for any words, case insensitive, starting with 'k' and ending in 's'
if ( eregi( "[[:<:]](k[[:alpha:]]+s)[[:>:]]", $states, $statesArray ) )
print( "Match found beginning with 'k' and ending in 's': " .
$statesArray[ 1 ] ."<br />");
//Search for any words starting with 'M' and ending in 's'
//if ( ereg( "[[:<:]](M[[:alpha:]]*s)[[:>:]]", $states, $statesArray ) )
//print( "Match found beginning with 'M' and ending in 's': " .
//$statesArray[ 2 ] ."<br />");
//Search for any words ending in 'a'
//if (ereg( "[[:<:]]([a-zA-Z]*a)[[:>:]]", $states, $statesArray ) )
//print( "Match found ending in 'a': " .
//$statesArray[ 3 ] ."<br />");
//while ( ereg("[[:<:]]([[:upper:]][[:lower:]])*M)[[:>:]]", $states, $statesArray ) )
//{
// print( $statesArray[ 4 ] . " " );
// $states = ereg_replace( $statesArray[ 4 ], "", $states );
//} // end while
//print( $statesArray[0], $statesArray[1], $statesArray[2], $statesArray[3], $statesArray[4], );
?><!-- end of PHP Script -->
I've gotta do five different searches. One that searches $states that ends in xas and stores it in element0. A second that searches $states that begins with k and ends in s, stored in element 1. A third search that begins with M and ends in s, stored in element2. A fourth search that ends in a, stored in element3. Finally, a fifth that searches the beginning of the string that starts with M.
I've worked out all the code the best I could, but only the ones not in comments work and I'm not sure exactly why the others are giving errors and why they're not being stored properly.
<?php
$states = "Mississippi Alabama Texas Massachusetts Kansas";
print("This script will search, '$states' <br /> <br />");
//Search for any words ending in 'xas'
if (ereg( "[[:<:]]([a-zA-Z]*xas)[[:>:]]", $states, $statesArray ) )
print( "Match found ending in 'xas': " .
$statesArray[ 0 ] ."<br />");
//Search for any words, case insensitive, starting with 'k' and ending in 's'
if ( eregi( "[[:<:]](k[[:alpha:]]+s)[[:>:]]", $states, $statesArray ) )
print( "Match found beginning with 'k' and ending in 's': " .
$statesArray[ 1 ] ."<br />");
//Search for any words starting with 'M' and ending in 's'
//if ( ereg( "[[:<:]](M[[:alpha:]]*s)[[:>:]]", $states, $statesArray ) )
//print( "Match found beginning with 'M' and ending in 's': " .
//$statesArray[ 2 ] ."<br />");
//Search for any words ending in 'a'
//if (ereg( "[[:<:]]([a-zA-Z]*a)[[:>:]]", $states, $statesArray ) )
//print( "Match found ending in 'a': " .
//$statesArray[ 3 ] ."<br />");
//while ( ereg("[[:<:]]([[:upper:]][[:lower:]])*M)[[:>:]]", $states, $statesArray ) )
//{
// print( $statesArray[ 4 ] . " " );
// $states = ereg_replace( $statesArray[ 4 ], "", $states );
//} // end while
//print( $statesArray[0], $statesArray[1], $statesArray[2], $statesArray[3], $statesArray[4], );
?><!-- end of PHP Script -->