Assignments from Class 10: Strings and String Functions in PHP

Rashmi Mishra
0

 Assignments from Class 10: 

Strings and String Functions in PHP

Here are some beginner-friendly assignments along with solutions based on string functions in PHP:


Assignment 1: Count the Occurrences of a Word

Task:
Write a PHP script that counts the number of times the word "PHP" appears in the sentence "PHP is a popular scripting language. PHP can be embedded into HTML. PHP is easy to learn."

Solution:

$text = "PHP is a popular scripting language. PHP can be embedded into HTML. PHP is easy to learn.";

$word = "PHP";

$count = substr_count($text, $word);

echo "The word 'PHP' appears " . $count . " times."; // Output: 3


Assignment 2: Extract Domain from Email

Task:
Write a PHP script that extracts the domain part of an email address. For example, if the email is user@example.com, the output should be example.com.

Solution:

$email = "user@example.com";

$domain = substr($email, strpos($email, '@') + 1);

echo "The domain is: " . $domain; // Output: example.com


Assignment 3: Replace All Occurrences of a Word

Task:
Write a PHP script that replaces all occurrences of the word "easy" with "fun" in the sentence "Learning PHP is easy and can be easy for everyone."

Solution:

$text = "Learning PHP is easy and can be easy for everyone.";

$updated_text = str_replace("easy", "fun", $text);

echo $updated_text; // Output: Learning PHP is fun and can be fun for everyone.


Assignment 4: Check if String Contains Another String

Task:
Write a PHP script that checks if the word "programming" exists in the string "PHP programming is powerful." Display a message indicating if it’s found or not.

Solution:

$text = "PHP programming is powerful.";

$word = "programming";

$position = strpos($text, $word);

 

if ($position !== false) {

    echo "The word 'programming' was found at position: " . $position;

} else {

    echo "The word 'programming' was not found.";

}

// Output: The word 'programming' was found at position: 4


Assignment 5: Extract the First 10 Characters of a String

Task:
Write a PHP script that extracts and prints the first 10 characters from the string "Mastering PHP programming is rewarding."

Solution:

$text = "Mastering PHP programming is rewarding.";

$substring = substr($text, 0, 10);

echo $substring; // Output: Mastering


Assignment 6: Reverse a String

Task:
Write a PHP script that reverses the string "Hello, World!" and prints it.

Solution:

$text = "Hello, World!";

$reversed_text = strrev($text);

echo $reversed_text; // Output: !dlroW ,olleH


Assignment 7: Count Vowels in a String

Task:
Write a PHP script that counts the number of vowels (a, e, i, o, u) in the string "The quick brown fox jumps over the lazy dog."

Solution:

$text = "The quick brown fox jumps over the lazy dog.";

$vowels_count = preg_match_all('/[aeiouAEIOU]/', $text);

echo "The number of vowels is: " . $vowels_count; // Output: 11


Assignment 8: Convert First Letter of Each Word to Uppercase

Task:
Write a PHP script that converts the first letter of each word in the sentence "php is a scripting language" to uppercase.

Solution:

$text = "php is a scripting language";

$formatted_text = ucwords($text);

echo $formatted_text; // Output: Php Is A Scripting Language


Assignment 9: Trim Extra Spaces from a String

Task:
Write a PHP script that trims all leading and trailing spaces from the string " PHP is fun! ".

Solution:

$text = "   PHP is fun!   ";

$trimmed_text = trim($text);

echo $trimmed_text; // Output: PHP is fun!


Assignment 10: Check if a String Starts with a Specific Word

Task:
Write a PHP script to check if the string "Learning PHP is enjoyable" starts with the word "Learning".

Solution:

$text = "Learning PHP is enjoyable";

$startsWithLearning = (strpos($text, "Learning") === 0);

 

if ($startsWithLearning) {

    echo "The string starts with 'Learning'.";

} else {

    echo "The string does not start with 'Learning'.";

}

// Output: The string starts with 'Learning'.


These assignments are designed to solidify understanding of PHP string manipulation functions and enhance familiarity with handling strings in PHP.


Post a Comment

0Comments

Post a Comment (0)