<?php //Use string functions for all the examples! //Complete the code and run i
ID: 3802883 • Letter: #
Question
<?php //Use string functions for all the examples! //Complete the code and run it. All output should //be numbered with the question, and skip lines //in the output between questions. // Post your completed //code to BB (attach a file), and upload your completed //php script, putting //the URL into the comments section on BB $haystack = "<title> PHP Coding </title>"; $needle = "<title>"; //1. print the position of the $needle in $haystack $newneedle = "</title>"; //2. print the position of $newneedle in $haytack //3. How long is the title in $haystack? Print the length //between the end of the open <title> tag and the beginning //of the close title tag! //let us look at a DNA example $haystack = "ACAAGACACATGCCACATTGTCC"; $needle = "ACA"; //4. print out the number of times $needle //occurs (nonoverlapping) in haystack //TT occurs once in TTT! //5. Print out the number of time $needle occurs in //$haystack. Count overlapping examples as well! //so TT occurs two times in TTT, for example! $subject = "Cloud computing works!"; //6. Print out the $subject with //all letter 'o' replaced with the letter 'e' $subject = "Keep your spacing perfect!"; //7. print out $subject where replace any two //spaces in a row ' ' with a - and // any single space ' ' with a * //so the example above prints //Keep*your--spacing-*perfect!"; $string = "red orange yellow green blue indigo violet"; //8. create an array that has the colors of the above string //in reverse order (so $colors[0] is 'violet') //You might need the array_reverse function //look it up on php.net //9. Print out the newly created array in the new order using //foreach, in UPPER CASE //Extra Credit! $personalize = "Joe;Sally;Jim;Sue;Alex;Maria"; $name = strtok($personalize,';'); //Extra Credit 1! Use a loop and strtok //to print out a message to each person in the //$personalize string above that says //Welcome back $name! Good to see you today! //Extra Credit 2! Do the same as above, but not using //strtok function ?>
Explanation / Answer
<?php
//Use string functions for all the examples!
//Complete the code and run it. All output should
//be numbered with the question, and skip lines
//in the output between questions.
// Post your completed
//code to BB (attach a file), and upload your completed
//php script, putting
//the URL into the comments section on BB
$haystack = "<title> PHP Coding </title>";
$needle = "<title>";
//1. print the position of the $needle in $haystack
$needlePos = strpos($haystack, $needle);/*String position count start from 0*/
echo "======$needle====position=====:",$needlePos; /*Output:0*/
$newneedle = "</title>";
//2. print the position of $newneedle in $haytack
$newneedlePos = strpos($haystack, $newneedle);
echo " ======$newneedle====position=====:",$newneedlePos;/*Output:19*/
//3. How long is the title in $haystack? Print the length
//between the end of the open <title> tag and the beginning
//of the close title tag!
$strparsed = stringInBetween($haystack, '<title>', '</title>');
echo " ======in between string======",$strparsed;
echo " =====length of string=======",strlen($strparsed); /*Output:12*/
//let us look at a DNA example
$haystack = "ACAAGACACATGCCACATTGTCC";
$needle = "ACA";
//4. print out the number of times $needle
//occurs (nonoverlapping) in haystack
//TT occurs once in TTT!
echo " =======$needle===occurence=====",substr_count($haystack, $needle); /*Output:3*/
//5. Print out the number of time $needle occurs in
//$haystack. Count overlapping examples as well!
//so TT occurs two times in TTT, for example!
echo " =======with overlapping========",strCountOverlap($haystack, $needle); /*Output:4*/
$subject = "Cloud computing works!";
//6. Print out the $subject with
//all letter 'o' replaced with the letter 'e'
echo " =====Replaced string=======",str_replace('o', 'e', $subject); /*O/P:Cleud cemputing werks!*/
$subject = "Keep your spacing perfect!";
//7. print out $subject where replace any two
//spaces in a row ' ' with a - and
// any single space ' ' with a *
//so the example above prints
//Keep*your--spacing-*perfect!";
$st1 = str_replace(' ', '-', $subject);
$subject = str_replace(' ', '*', $st1);
echo " ===========",$subject;/*O/P:Keep*your--spacing-*perfect!*/
$string = "red orange yellow green blue indigo violet";
//8. create an array that has the colors of the above string
//in reverse order (so $colors[0] is 'violet')
//You might need the array_reverse function
//look it up on php.net
$newarray = explode(" ", $string);
echo "=======newarray before ordered=====";
print_r($newarray);
$reversed = array_reverse($newarray);
echo " =======newarray after ordered=====";
print_r($reversed);
/*
O/P:
=======newarray before ordered=====Array
(
[0] => red
[1] => orange
[2] => yellow
[3] => green
[4] => blue
[5] => indigo
[6] => violet
)
=======newarray after ordered=====Array
(
[0] => violet
[1] => indigo
[2] => blue
[3] => green
[4] => yellow
[5] => orange
[6] => red
)
*/
//9. Print out the newly created array in the new order using
//foreach, in UPPER CASE
$newarray = array();
foreach($reversed as $key=>$values)
{
$newarray[$key] = strtoupper($values);
}
print_r($newarray);
/*
o/p:
Array
(
[0] => VIOLET
[1] => INDIGO
[2] => BLUE
[3] => GREEN
[4] => YELLOW
[5] => ORANGE
[6] => RED
)
*/
//Extra Credit!
$personalize = "Joe;Sally;Jim;Sue;Alex;Maria";
$name = strtok($personalize,';');
//Extra Credit 1! Use a loop and strtok
//to print out a message to each person in the
//$personalize string above that says
//Welcome back $name! Good to see you today!
while ($name !== false)
{
$name = strtok(";");
echo "Welcome back $name! Good to see you today! ";
}
/*
o/p:
Welcome back Sally! Good to see you today!
Welcome back Jim! Good to see you today!
Welcome back Sue! Good to see you today!
Welcome back Alex! Good to see you today!
Welcome back Maria! Good to see you today!
Welcome back ! Good to see you today!
*/
//Extra Credit 2! Do the same as above, but not using
//strtok function
$personalize = "Joe;Sally;Jim;Sue;Alex;Maria";
$personArray = explode(';',$personalize);
foreach($personArray as $k=>$name)
{
echo "Welcome back $name! Good to see you today! ";
}
/*
o/p:
Welcome back Sally! Good to see you today!
Welcome back Jim! Good to see you today!
Welcome back Sue! Good to see you today!
Welcome back Alex! Good to see you today!
Welcome back Maria! Good to see you today!
Welcome back ! Good to see you today!
*/
function stringInBetween($mystr, $start, $end){
$mystr = ' ' . $mystr;
$ini = strpos($mystr, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($mystr, $end, $ini) - $ini;
return substr($mystr, $ini, $len);
}
function strCountOverlap($haystack, $needle) {
$count = 0;
$start = 0;
while(1) {
$found = strpos($haystack, $needle, $start);
if($found !== FALSE) {
$count++;
$start = $found + 1;
} else return $count;
}
return $count;
}
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.