Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

Useful regular expressions

Some useful PHP regular expressions: Find all emails in text:
preg_match_all('/([\s]*)[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i', $html, $matches);
Check that string is allowed to contain Cyrillic symbols, Latin symbols and numbers:
preg_match('/^[a-zA-Z\p{Cyrillic}\d\s\-]+$/u', $str);
Get anchors URLs in HTML:
preg_match_all("/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU", $html, $matches); 
Get src, title and alt of an image tag:
preg_match_all('/(src|title|alt)=("[^"]*")/i', $html, $matches);
Replace multiple intervals with single one:
preg_replace('!\s+!', ' ', $string); 
Replace anything but letters of any alphabet, spaces and numbers:
preg_replace('/[^ \w]+/u', '', $str); 
Search for a string in array of values: preg_grep("/{$search}/i", $array); To be continued...