PHP Regular Expression also known as regex are powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions. They can help you accomplish tasks such as validating email addresses, IP address etc.

preg_match('/^[\pL]+$/u', $case); //allow multi language only
preg_match('/^[\pL\s]+$/u', $case); //allow multi language and spaces
Example:

"愛してます" letters only?
true
"我爱你" letters only?
true

Why use regular expressions

  • PHP Regular expressions simplify identifying patterns in string data by calling a single function. This saves us coding time.
  • When validating user input such as email address, domain names, telephone numbers, IP addresses,
  • Highlighting keywords in search results
  • When creating a custom HTML template. Regex in PHP can be used to identify the template tags and replace them with actual data.

Regular Expression Metacharacters

The above examples used very basic patterns; metacharacters simply allow us to perform more complex pattern matches such as test the validity of an email address. Let’s now look at the commonly used metacharacters.

MetacharacterDescriptionExample
.Matches any single character except a new line/./ matches anything that has a single character
^Matches the beginning of or string / excludes characters/^PH/ matches any string that starts with PH
$Matches pattern at the end of the string/com$/ matches guru99.com,yahoo.com Etc.
*Matches any zero (0) or more characters/com*/ matches computer, communication etc.
+Requires preceding character(s) appear at least once/yah+oo/ matches yahoo
\Used to escape meta characters/yahoo+\.com/ treats the dot as a literal value
[…]Character class/[abc]/ matches abc
a-zMatches lower case letters/a-z/ matches cool, happy etc.
A-ZMatches upper case letters/A-Z/ matches WHAT, HOW, WHY etc.
0-9Matches any number between 0 and 9/0-4/ matches 0,1,2,3,4
Regex Metacharacter Table

Difference of preg_match() and preg_match_all() functions

preg_match() stops looking after the first match. preg_match_all(), on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.


<?php 
// TESTCASES FOR ALL UNICODE GOOGLE TRANSLATE LANGUAGE 2023
// ALLOWING MULTI LANGUAGE AND SPACES
$testCases = [ 'Unë të dua', 'i love you', 'አፈቅርሃለሁ', 'Ես սիրում եմ քեզ', 'মই আপোনাক ভাল পাওঁ', 'munsmawa', 'Mən səni sevirəm', "n b'i fɛ", 'я цябе кахаю', 'আমি তোমাকে ভালোবাসি', 'हम तोहसे प्यार करेलीं', 'Обичам те', "T'estimo", 'Miluji tě', 'އަހަރެން ތިބާ ދެކެ ލޯބިވަން', 'में तुगी हिरख करना', 'Melɔ̃ wò', 'Minä rakastan sinua', 'Ik hâld fan dy', 'Quérote', 'მიყვარხარ', "Σ'αγαπώ", 'હું તને પ્રેમ કરું છુ', 'Aloha wau iā ʻoe', 'אני אוהב אותך', 'मुझे तुमसे प्यार है', 'ég elska þig', "a hụrụ m gị n'anya", 'Ay-ayaten ka', 'Is breá liom tú', 'ನಾನು ನಿನ್ನನ್ನು ಪ್ರೀತಿಸುತ್ತೇನೆ', 'мен сені жақсы көремін', 'ខ្ញុំ​ស្រលាញ់​អ្នក', 'हांव तुजेर मोग करता', '사랑해요', 'A lɛk yu', 'تۆم خۆش دەوێت', 'Сени сүйөм', 'ຂ້ອຍ​ຮັກ​ເຈົ້າ', 'Es mīlu Tevi', 'Aš tave myliu', 'Ech hunn dech gär', 'Те сакам', 'हम अहां सँ प्रेम करैत छी', 'ഞാൻ നിന്നെ സ്നേഹിക്കുന്നു', 'inħobbok', 'मी तुझ्यावर प्रेम करतो', 'Би чамд хайртай', 'ମୁଁ ତୁମକୁ ଭଲପାଏ', 'မင်းကိုချစ်တယ်', 'म तिमीलाई माया गर्छु', 'Kocham cię', 'jag älskar dig', 'мин сине яратам', 'నేను నిన్ను ప్రేమిస్తున్నాను', 'مەن سېنى ياخشى كۆرىمەن', 'Tha gaol agam ort', 'Kuyaykim', 'أحبك', '愛してます', '我爱你', 'Anh Yêu Em', 'ฉันรักคุณ',
];
foreach($testCases as $case) {
    echo '<b>"' . $case . '" letters only? </b><br>' . var_export((bool) preg_match('/^[\pL\s]+$/u', $case), TRUE) . "\n<br><br>";
}

Result:

"Unë të dua" letters only?
true

"i love you" letters only?
true

"አፈቅርሃለሁ" letters only?
true

"Ես սիրում եմ քեզ" letters only?
true

"মই আপোনাক ভাল পাওঁ" letters only?
false

"munsmawa" letters only?
true

"Mən səni sevirəm" letters only?
true

"n b'i fɛ" letters only?
false

"я цябе кахаю" letters only?
true

"আমি তোমাকে ভালোবাসি" letters only?
false

"हम तोहसे प्यार करेलीं" letters only?
false

"Обичам те" letters only?
true

"T'estimo" letters only?
false

"Miluji tě" letters only?
true

"އަހަރެން ތިބާ ދެކެ ލޯބިވަން" letters only?
false

"में तुगी हिरख करना" letters only?
false

"Melɔ̃ wò" letters only?
false

"Minä rakastan sinua" letters only?
true

"Ik hâld fan dy" letters only?
true

"Quérote" letters only?
true

"მიყვარხარ" letters only?
true

"Σ'αγαπώ" letters only?
false

"હું તને પ્રેમ કરું છુ" letters only?
false

"Aloha wau iā ʻoe" letters only?
true

"אני אוהב אותך" letters only?
true

"मुझे तुमसे प्यार है" letters only?
false

"ég elska þig" letters only?
true

"a hụrụ m gị n'anya" letters only?
false

"Ay-ayaten ka" letters only?
false

"Is breá liom tú" letters only?
true

"ನಾನು ನಿನ್ನನ್ನು ಪ್ರೀತಿಸುತ್ತೇನೆ" letters only?
false

"мен сені жақсы көремін" letters only?
true

"ខ្ញុំ​ស្រលាញ់​អ្នក" letters only?
false

"हांव तुजेर मोग करता" letters only?
false

"사랑해요" letters only?
true

"A lɛk yu" letters only?
true

"تۆم خۆش دەوێت" letters only?
true

"Сени сүйөм" letters only?
true

"ຂ້ອຍ​ຮັກ​ເຈົ້າ" letters only?
false

"Es mīlu Tevi" letters only?
true

"Aš tave myliu" letters only?
true

"Ech hunn dech gär" letters only?
true

"Те сакам" letters only?
true

"हम अहां सँ प्रेम करैत छी" letters only?
false

"ഞാൻ നിന്നെ സ്നേഹിക്കുന്നു" letters only?
false

"inħobbok" letters only?
true

"मी तुझ्यावर प्रेम करतो" letters only?
false

"Би чамд хайртай" letters only?
true

"ମୁଁ ତୁମକୁ ଭଲପାଏ" letters only?
false

"မင်းကိုချစ်တယ်" letters only?
false

"म तिमीलाई माया गर्छु" letters only?
false

"Kocham cię" letters only?
true

"jag älskar dig" letters only?
true

"мин сине яратам" letters only?
true

"నేను నిన్ను ప్రేమిస్తున్నాను" letters only?
false

"مەن سېنى ياخشى كۆرىمەن" letters only?
true

"Tha gaol agam ort" letters only?
true

"Kuyaykim" letters only?
true

"أحبك" letters only?
true

"愛してます" letters only?
true

"我爱你" letters only?
true

"Anh Yêu Em" letters only?
true

"ฉันรักคุณ" letters only?
false