Questions tagged [preg-match]

Regular expression pattern matching function for the PHP programming language

The preg_match() function in searches for a match to the regular expression given using Perl Compatible Regular Expression (PCRE) syntax. Specifically, it is for matching one value or one set of data with a given pattern.

If you require multiple matches that match your pattern, you will want to use the function

Description

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Basic matching

preg_match('/PHP/', 'PHP')       # Match for an unbound literal
preg_match('/^PHP/', 'PHP')      # Match literal at start of string
preg_match('/PHP$/', 'PHP')      # Match literal at end of string
preg_match('/^PHP$/', 'PHP')     # Match for exact string content
preg_match('/^$/', '')           # Match empty string

Using different regular expression delimiters

preg_match('/PHP/', 'PHP')       # / as commonly used delimiter
preg_match('@PHP@', 'PHP')       # @ as delimiter
preg_match('!PHP!', 'PHP')       # ! as delimiter 

References

5363 questions
31
votes
5 answers

Are the PHP preg_functions multibyte safe?

There are no multibyte 'preg' functions available in PHP, so does that mean the default preg_functions are all mb safe? Couldn't find any mention in the php documentation.
Spoonface
  • 1,513
  • 1
  • 20
  • 29
28
votes
2 answers

check if the string contains alphabets in php

How to check if the string has alphabets in PHP The ctype_alpha and ctype_digit doesn't help in it. is their any method?
user1765876
27
votes
2 answers

PHP preg_match to find whole words

I'm quite new to regular expressions. Could you help me to create a pattern, which matches whole words, containing specific part? For example, if I have a text string "Perform a regular expression match" and if I search for express, it shuld give me…
tintix
  • 373
  • 1
  • 3
  • 4
25
votes
2 answers

Troubleshooting "Delimiter must not be alphanumeric or backslash" error when changing ereg() to preg_match()

Possible Duplicate: Converting ereg expressions to preg
robpal
  • 846
  • 4
  • 10
  • 21
25
votes
3 answers

Preg match text in php between html tags

Hello I would like to use preg_match in PHP to parse the "Desired text" out of the following from a html document

Desired text

Ordinarily I would use simple_html_dom for such things but on this occasion it cannot be used…
David Willis
  • 1,082
  • 7
  • 15
  • 23
24
votes
9 answers

preg: how to write a preg pattern to get domain name from an email?

From an email address like something@gmail.com I want to fetch domain name gmail.com. i want to use that pattern on textbox value in Javascript. Pease suggest me an optimized and faster preg pattern for above requirement...
Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56
24
votes
8 answers

php explode: split string into words by using space a delimiter

$str = "This is a string"; $words = explode(" ", $str); Works fine, but spaces still go into array: $words === array ('This', 'is', 'a', '', '', '', 'string');//true I would prefer to have words only with no spaces and keep the information…
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
23
votes
9 answers

How to return only named groups with preg_match or preg_match_all?

Example: $string = "This is some text written on 2010-07-18."; preg_match('|(?\d\d\d\d-\d\d-\d\d)|i', $string, $arr_result); print_r($arr_result); Returns: Array ( [0] => 2010-07-18 [date] => 2010-07-18 [1] => 2010-07-18 ) But I…
rsk82
  • 28,217
  • 50
  • 150
  • 240
22
votes
7 answers

How to validate a domain name using Regex & PHP?

I want a solution to validate only domain names not full URLs, The following example is what I'm looking for: example.com -> true example.net -> true example.org -> true example.biz -> true example.co.uk -> true sub.example.com ->…
CodeOverload
  • 47,274
  • 54
  • 131
  • 219
20
votes
5 answers

Maximum Hex value in regex

Without using u flag the hex range that can be used is [\x{00}-\x{ff}], but with u flag it goes up to a 4-byte value \x{7fffffff} ([\x{00000000}-\x{7fffffff}]). So if I execute the below code: preg_match("/[\x{00000000}-\x{80000000}]+/u", $str,…
revo
  • 47,783
  • 14
  • 74
  • 117
20
votes
5 answers

PHP crashes on preg_replace

I ran the following script using php.exe: preg_replace('#(?:^[^\pL]*)|(?:[^\pL]*$)#u','',$string); or its equivalent: preg_replace('#(?:^[^\pL]*|[^\pL]*$)#u','',$string); If $string="S" or $string=" ذذ " it works, if string='ذ' it yields �…
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
19
votes
3 answers

Regex Optional Groups?

I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated. My current regex is as follows: /(.*?)/se I am looking to make the number input and…
Joel
  • 2,185
  • 4
  • 29
  • 56
18
votes
3 answers

PHP regex non-capture non-match group

I'm making a date matching regex, and it's all going pretty well, I've got this so far: "/(?:[0-3])?[0-9]-(?:[0-1])?[0-9]-(?:20)[0-1][0-9]/" It will (hopefully) match single or double digit days and months, and double or quadruple digit years in…
Ben
  • 54,723
  • 49
  • 178
  • 224
18
votes
1 answer

preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 27 on line number 25

I am writing up some PHP code. In this code, I am running a for loop within a for loop to iterate over an array, then to iterate over the characters in the current string in the array. I then want to do preg_match() on the current string to see if…
Tim
  • 649
  • 1
  • 5
  • 21
18
votes
1 answer

PHP preg_match to find multiple occurrences

What is the correct syntax for a regular expression to find multiple occurrences of the same string with preg_match in PHP? For example find if the following string occurs TWICE in the following paragraph: $string = "/brown fox jumped…
Marcus
  • 4,400
  • 13
  • 48
  • 64