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
18
votes
6 answers

Are preg_match() and preg_replace() slow?

I've been coding in PHP for a while and I keep reading that you should only use preg_match and preg_replace when you have to because it slows down performance. Why is this? Would it really be bad to use 20 preg_matches in one file instead of using…
Jasko Koyn
  • 249
  • 1
  • 3
  • 9
18
votes
5 answers

PHP preg_match get in between string

I'm trying to get the string hello world. This is what I've got so far: $file = "1232#hello world#"; preg_match("#1232\#(.*)\##", $file, $match)
Kevin King
  • 557
  • 1
  • 9
  • 25
18
votes
2 answers

preg_match with international characters and accents

I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok". I don't know what I am doing wrong, can you help? thanks $string =…
Vincent
  • 1,651
  • 9
  • 28
  • 54
17
votes
2 answers

Is it safe to use user's RegEx?

I want to add a feature to my website to let users search the texts with RegEx. But, is it safe to let the users do something like that ? preg_match('/' . $user_input_regex . '/', $subject);
Sdgsdg Asgasgf
  • 205
  • 2
  • 6
17
votes
3 answers

Extracting all values between curly braces regex php

I have content in this form $content ="

This is a sample text where {123456} and {7894560} ['These are samples']{145789}

"; I need all the values between curly braces in an array like the one shown…
Ganesh Babu
  • 3,590
  • 11
  • 34
  • 67
17
votes
6 answers

Replacing Placeholder Variables in a String

Just finished making this function. Basically it is suppose to look through a string and try to find any placeholder variables, which would be place between two curly brackets {}. It grabs the value between the curly brackets and uses it to look…
Tyler Hughes
  • 582
  • 1
  • 9
  • 19
17
votes
7 answers

PHP string console parameters to array

I would like to know how I could transform the given string into the specified array: String all ("hi there \(option\)", (this, that), other) another Result wanted (Array) [0] => all, [1] => Array( [0] => "hi there \(option\)", [1] =>…
Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53
16
votes
3 answers

Counterpart to PHP’s preg_match in Python

I am planning to move one of my scrapers to Python. I am comfortable using preg_match and preg_match_all in PHP. I am not finding a suitable function in Python similar to preg_match. Could anyone please help me in doing so? For example, if I want to…
funnyguy
  • 513
  • 3
  • 6
  • 15
16
votes
3 answers

using preg_match to detect persian (farsi) characters in string

I am trying to validate form data from server-side. my interest is that the user just fill the form by Persian characters. I am using this code: $name=trim($_POST['name']); $name= mysql_real_escape_string($name); if…
John
  • 2,461
  • 5
  • 21
  • 18
16
votes
6 answers

Matching SRC attribute of IMG tag using preg_match

I'm attempting to run preg_match to extract the SRC attribute from the first IMG tag in an article (in this case, stored in $row->introtext). preg_match('/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\']*)/i', $row->introtext, $matches); Instead of…
KyokoHunter
  • 387
  • 1
  • 4
  • 15
16
votes
5 answers

Regex for finding valid filename

I want to check whether a string is a file name (name DOT ext) or not. Name of file cannot contain / ? * : ; { } \ Could you please suggest me the regex expression to use in preg_match()?
OrangeRind
  • 4,798
  • 13
  • 45
  • 57
15
votes
4 answers

Detecting emails in a text

I'm trying to create a function that translates every occurrence of a plain text email address in a given string into it's htmlized version. Let's say I have the following code, where htmlizeEmails is the function I'm looking for: $str = "Send me an…
federico-t
  • 12,014
  • 19
  • 67
  • 111
15
votes
4 answers

preg_match php street address

I need to match using regular expression in php addresses like: 144 street, city, state zip/postal code 144 street, apt #1, city, state zip/postal code 144 street apt #1, city state zip/postal code The zip/postal code can includes letters and/or…
Marty
  • 555
  • 1
  • 5
  • 11
15
votes
2 answers

PHP preg_match UUID v4

I've got a string that contains UUID v4 $uuid = 'http://domain.com/images/123/b85066fc-248f-4ea9-b13d-0858dbf4efc1_small.jpg'; How would i get the b85066fc-248f-4ea9-b13d-0858dbf4efc1 value from the above using preg_match()? More info on UUID v4…
Pav
  • 2,288
  • 4
  • 22
  • 25
15
votes
3 answers

Regex - match anything except specific string

I need a regex (will be used in ZF2 routing, I believe it uses the preg_match of php) that matches anything except a specific string. For example: I need to match anything except "red", "green" or "blue". I currently have the regex:…
rafaame
  • 822
  • 2
  • 12
  • 22