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
1
vote
2 answers

how to use special expression using preg_match_all in php

I want to preg_match_all() this: if(isset($matches[1])){ return $matches[1]; }else{ return false; } } $lines = file('jason.txt'); $i=0; foreach ($lines as $line_num => $line) { $str_arr =…
1
vote
1 answer

How to split out string by symbols using preg_split?

I have for example string $s = 'A5C7'; and $s = 'A5C17'; I was try split string by symbols using preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY); If string $s = 'A5C7' I got OK result like this: array(4) { [0]=> string(1) "A" [1]=> string(1)…
Rider_BY
  • 1,129
  • 1
  • 13
  • 31
1
vote
1 answer

How to exclude cases in php regex

I am trying to capture specific data where a colon exists. I have tried this: preg_match_all("/^(.+):(.+)/im", $input_lines, $output_array); on this input data last_name, first_name bjorge philip: hello world bjorge:world kardashian, kim some…
Kal
  • 948
  • 17
  • 30
1
vote
2 answers

Regex/preg_match - get text between string and email address

I'm extracting data from emails. I have pieces of text like this: Eg. 1: some standard text. Bugs Bunny bugs@gmail.com 0411111111 more standard text Eg. 2: some standard text. Bugs The Bunny bugs@gmail.com 0411111111 more standard text Eg. 3:…
Warren
  • 1,984
  • 3
  • 29
  • 60
1
vote
2 answers

preg_match not working properly

i have some data and i want to find out value of it so i try preg_match function it can be easy to finding values i try the the following code to obtain values but i think there is some problem in my code because i am getting value of $a but i try…
1
vote
1 answer

preg match for registration script php

Im trying to do some preg match to make my script more secure, but whatever i type in username/realname i get error... where am i going wrong here? $unpreg = $_POST["username"]; $empreg = $_POST["email"]; $rnpreg =…
per källström
  • 169
  • 2
  • 11
1
vote
1 answer

php: preg_match: stop searching if any element of pattern exists within text

$pattern = '/^(^[\`\~\@\#\$\%\^\&\\])+$/'; if(preg_match($pattern, $textToSearch)){ exit('Bad text.'); } The code above is supposed to exit upon the first occurrence of any element in the pattern above. But it is not working. Will anyone please…
sam
  • 11
  • 1
1
vote
2 answers

preg_match regex with fix character in the middle

I'm using preg_match to find two letters and 4-6 digits, but there should be a hyphen - character in the middle. I'm starting from there: if (preg_match("/^[a-zA-Z]{2}\W[0-9]{4,6}$/"... I came out with this solution, where I use \W as an any…
syluccy
  • 21
  • 3
1
vote
1 answer

Split address street name house number and room number

I need split address: Main Str. 202-52 into street=Main Str. house No.=202 room No.=52 I tried to use this: $data['address'] = "Main Str. 202-52"; $data['street'] = explode(" ", $data['address']); $data['building'] = explode("-",…
K. B.
  • 1,388
  • 2
  • 13
  • 25
1
vote
1 answer

Lazy Load Background Images Wordpress

I want to lazy load background images so i need to convert
Niresh
  • 135
  • 1
  • 2
  • 13
1
vote
1 answer

Get error in using skyscanner Api of flight pricing in php

i am following link for accessing flight live prices Skyscanner API for all airport locations and Travel API using CURL i have replace api key with my apikey. PHP Code:
umer safeer
  • 69
  • 2
  • 16
1
vote
3 answers

strip out all strange characters of string

I'm trying to create a slug so I would like to strip out every strange character. The only thing the slug should contain is lowercase letters and underscores. Is there a way to check for strange characters and filter it out the string? everything…
Christophe
  • 4,798
  • 5
  • 41
  • 83
1
vote
3 answers

Remove unwanted characters in given format

I have one string format in that remove unwanted characters and taken time format only my string is 10:02:in(user) in that remove all the characters and taken 10:02 only how?
rajsree
  • 41
  • 8
1
vote
1 answer

PHP preg match exactly

I do have the following pregmatch: $search = 310850; if (preg_match("/\b$search\b/i", $response)) { echo "match !"; } else { echo "NO MATCH"; } response contains alot of things, but it contains 310850 and also 3108502 and…
maria
  • 207
  • 5
  • 22
  • 56
1
vote
2 answers

PHP preg match to validate user text input

I need to validate a user input of about 1,500 characters Allowed characters are a-z A-Z 0-9 Allowed symbols . , : ? ! and space Can anyone suggest a preg_match expression for this? I am using '/^[a-zA-Z0-9.,:?! ]+$/' but it gives error sometimes
Jorge
  • 109
  • 1
  • 1
  • 6
1 2 3
99
100