POSIX based regular expressions used in PHP are the second most common syntax being used for regular expressions. Integrate functions and properties handling the PHP POSIX based regular expressions are shown in examples below.
POSIX based functions:
The ereg() function
The ereg() function has the same functionality as preg_match() in PERL based REGEX. PHP manual brings a very good example of code snippet that takes a date in ISO format (YY-MM-DD) and prints it in DD.MM.YYYY format:
Example with ereg() function and date format conversion
// Finding 'php' in the string
// 'i' means CASE SENSITIVE
if (ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo $reg[3] . $reg[2] . $regs[1];
}
else {
echo "Ivalid date format:$date";
}
The ereg_replace() function
The ereg_replace() function performs a regular expression search and replace a string defined in the string pattern. It works same as preg_replace() in PERL based REGEX.
Example with ereg_replace() function
<?php
$var = "Example of ereg_replacein php";
echo ereg_replace("( )php", "\\1php", $var);
?>
The split() function
The split() function splits a string defined in the string pattern and saves it's output in an array. It works same as split() in PERL based REGEX.
Example with split() function
<?php
$date = "03/08/2017";
list($month, $day, $year) = split('[/]', $date);
echo "Month: $month; Day: $day; Year: $year \n";
?>
Comments
No comments have been made yet.
Please login to leave a comment. Login now