Tutorial by Examples

preg_match checks whether a string matches the regular expression. $string = 'This is a string which contains numbers: 12345'; $isMatched = preg_match('%^[a-zA-Z]+: [0-9]+$%', $string); var_dump($isMatched); // bool(true) If you pass in a third parameter, it will be populated with the matchi...
$string = "0| PHP 1| CSS 2| HTML 3| AJAX 4| JSON"; //[0-9]: Any single character in the range 0 to 9 // + : One or more of 0 to 9 $array = preg_split("/[0-9]+\|/", $string, -1, PREG_SPLIT_NO_EMPTY); //Or // [] : Character class // \d : Any digit // + : One or more ...
$string = "a;b;c\nd;e;f"; // $1, $2 and $3 represent the first, second and third capturing groups echo preg_replace("(^([^;]+);([^;]+);([^;]+)$)m", "$3;$2;$1", $string); Outputs c;b;a f;e;d Searches for everything between semicolons and reverses the order.
A global RegExp match can be performed using preg_match_all. preg_match_all returns all matching results in the subject string (in contrast to preg_match, which only returns the first one). The preg_match_all function returns the number of matches. Third parameter $matches will contain matches in f...
preg_replace_callback works by sending every matched capturing group to the defined callback and replaces it with the return value of the callback. This allows us to replace strings based on any kind of logic. $subject = "He said 123abc, I said 456efg, then she said 789hij"; $regex = &q...

Page 1 of 1