"> ">
Download presentation
Presentation is loading. Please wait.
Published bySilvia Randall Modified over 9 years ago
1
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional
2
PHP’s Regular Expression Functions (Perl Compatible) preg_grep() preg_match() preg_match_all() preg_quote() preg_replace()
3
preg_grep() array preg_grep(string pattern, array input [, flags]) searches all elements of an array, returning an array consisting of all elements matching a certain pattern. <?php $foods = array("pasta", "steak", "fish", "potatoes"); $food = preg_grep("/^p/", $foods); print_r($food); ?>
4
preg_match() int preg_match(string pattern, string string [, array matches] [, int flags [, int offset]]]) searches a string for a specific pattern, returning TRUE if it exists, and FALSE otherwise. <?php $line = "vim is the greatest word processor ever created!"; if (preg_match("/\bVim\b/i", $line, $match)) print "Match found!"; ?>
5
preg_match_all() int preg_match_all(string pattern, string string, array pattern_array [, int order]) matches all occurrences of a pattern in a string, assigning each occurrence to an array in the order you specify via an optional input parameter. <?php $userinfo = "Name: Zeev Suraski Title: PHP Guru "; preg_match_all("/ (.*) /U", $userinfo, $pat_array); printf("%s %s", $pat_array[0][0], $pat_array[0][1]); ?>
6
preg_quote() string preg_quote(string str [, string delimiter]) inserts a backslash delimiter before every character of special significance to regular expression syntax. These special characters include $ ^ * ( ) + = { } [ ] | \\ : <?php $text = "Tickets for the bout are going for $500."; echo preg_quote($text); ?>
7
preg_replace() mixed preg_replace(mixed pattern, mixed replacement, mixed str [, int limit]) replaces all occurrences of pattern with replacement, and returning the modified result <?php $text = "This is a link to http://www.wjgilmore.com/."; echo preg_replace("/http:\/\/(.*)\//", " \${0} ", $text); ?>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.