Web Technology II · Unit 5
String Processing and Regular Expressions
Exam-focused notes for String Processing and Regular Expressions (Web Technology II, BIT301): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.
String Processing and Regular Expressions in short
Text handling is a big part of web programming. This chapter covers regular expressions, splitting and joining strings, comparing strings, and reading CSV text.
What this unit covers
- String patterns and regular expressions
- String exploding and imploding
- String comparison and approximate equality
- Character access in strings
- String manipulation functions
- CSV file parsing
Character access in strings
2082 · 5 marks
Write a program that will read your name as input string. Access and display the individual characters in the name string. [5]
String exploding and imploding
2081 · 5 marks
What is exploding and imploding of strings? Illustrate with an example. [5]
Show solved answerHide answer
Exploding and Imploding of Strings in PHP
1. Exploding a String
Exploding means splitting a string into an array based on a specified delimiter/separator. In PHP, this is done using the built-in explode() function.
Syntax:
explode(separator, string, limit)
separator- the delimiter used to split the string (required)string- the input string to be split (required)limit- maximum number of elements in the array (optional)
Example:
<?php
$str = "Apple,Banana,Mango,Orange";
$fruits = explode(",", $str);
print_r($fruits);
?>
Output:
Array
(
[0] => Apple
[1] => Banana
[2] => Mango
[3] => Orange
)
Here, the string is split at every comma , and each part becomes an element of the array.
2. Imploding a String
Imploding means joining array elements into a single string using a specified separator/glue. In PHP, this is done using the built-in implode() function (also called join()).
Syntax:
implode(separator, array)
separator- the string placed between each array element (required)array- the array whose elements are to be joined (required)
Example:
<?php
$fruits = array("Apple", "Banana", "Mango", "Orange");
$str = implode(" - ", $fruits);
echo $str;
?>
Output:
Apple - Banana - Mango - Orange
Here, the array elements are joined together with - as the separator to form a single string.
Summary Table
| Feature | explode() | implode() |
|---|---|---|
| Purpose | Splits a string into an array | Joins an array into a string |
| Input | String | Array |
| Output | Array | String |
| Also known as | Split | Join |
Key Point:
explode()andimplode()are inverse operations of each other. Explode breaks a string apart; implode brings array pieces together.
String comparison and approximate equality
2080 · 5 marks
How is the approximate equality of two strings done in PHP? Write a program to demonstrate it. [5]
String patterns and regular expressions
0 · 5 marks
Write a PHP program to illustrate string patterns using regular expression. [5]
Make Unit 5 stick
Practice BIT301 with flashcards & quizzes