Page 324 - Computer Software Application TP - Volume 1
P. 324
COMPUTER SOFTWARE APPLICATION - CITS
V Using preg_grep() for filtering elements of an array
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> Using preg_grep() </title>
</head>
<body>
<?php
// Sample array containing strings
$array = array(
‘apple’,
‘banana’,
‘orange’,
‘grape’,
‘watermelon’,
‘kiwi’,
‘strawberry’
);
// Regular expression pattern to match fruits containing ‘a’ followed by any character
$pattern = ‘/a./’;
// Use preg_grep() to filter array elements based on the pattern
$filteredArray = preg_grep($pattern, $array);
// Output the filtered array
echo “Filtered Array:\n”;
print_r($filteredArray);
?>
</body>
</html>
3 Save the program in C:\Apache24\htdocs in a folder with .php extension
4 Run the Apache services from windows services
5 Open the browser and type the following address
http://localhost/foldername/
6 Click the php file to run and verify the output
VI. Using preg_quote() for escapes special characters in a string
1 Open the text editor
2 Write the following codes
309
CITS : IT & ITES - Computer Software Application - Exercise 58