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
5 answers

PHP How to merge array element with next while maintaining order?

$array = ['coke.','fanta.','chocolate.']; foreach ($array as $key => $value) { if (strlen($value)<6) { $new[] = $value." ".$array[$key+1]; } else { $new[] = $value; } } This code doesn't have the desired effect, in fact it…
Hasen
  • 11,710
  • 23
  • 77
  • 135
1
vote
4 answers

Small number detecting regex

I need it to detect something like STEAM_numberhere 0 to 5:numberhere only 0 or 1:any number here PHP preg_match Thanks for your help! STEAM_X:Y:Z X has to be 0, 1, 2, 3, 4 or 5 Y has to be either 0 or 1 Z can be any number, just can't contain any…
Synergy
  • 13
  • 3
1
vote
2 answers

Error in preg_match PHP

user370306
1
vote
2 answers

How would I make this preg_match() reg expression ignore whitespace?

I have this regular expression: if(preg_match("/^(\\d+)(,)(\\d+)$/") ) { ... } which tests that the user has entered a pair of numbers separated by a comma (e.g. 120,80). I've got that part working properly. However I anticipate that users may…
orbit82
  • 513
  • 3
  • 6
  • 17
1
vote
1 answer

regex for pow()

First of all I just want to say that I never understood so well the regular expressions patterns, but I'm progressing :) I want to create a function for replacing the "^" char and left right matches with pow($1, $2). I manged to get to a point that…
Azrael
  • 193
  • 8
1
vote
1 answer

Warning: preg_match_all(): Unknown modifier '4'

I have the following error: Warning: preg_match_all(): Unknown modifier '4' in file.php on line 410 Code on line 410: preg_match_all("#$replacement_pattern#is", $text, $arr); Can someone please help me to fix this problem?
dawidex44
  • 11
  • 1
1
vote
2 answers

foreach and preg_match on a heavy amount of data not working properly

I have to files, one is full of keywords sequences (~20k lines), the other is full of regular expression (~2.5k). I want to test each keyword with each regexp and print the one that matches. I tested my files and that makes around 22 750 000 tests.…
Gabriel S.
  • 1,961
  • 2
  • 20
  • 30
1
vote
2 answers

Preg Match All into single variables

im really stuck in my code. I have a text thats looks like this [[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car And I need the text behind the [[QVQ]]. I read about preg…
1
vote
2 answers

Help me understand this PHP code

I am a PHP beginner. When going through a PHP script I found: if(preg_match('/(?i)ID=(\d+)/',$input)) { // id found } I want to know what does (?i) mean ?
user548219
  • 38
  • 4
1
vote
1 answer

preg_match to match url with multiple languages

i was using standard preg_match for making url excluding http://domainlllll.com/ and it was working without any issue preg_match("/^[0-9a-z_\/.\|\-]+$/",$url) but now i want to support multiple languages so i used this and it is also working…
swetlana
  • 47
  • 7
1
vote
1 answer

How to find number in text with pattern and change it in JavaScript

I have some text in text input that contain numbers with a pattern in whole of text and want to find all number with this pattern and recalculate and replace in text .... val="2xf" ......... vc="2.6xf" ...... value="1.2xf" ..... find all…
Mohammad Javad
  • 573
  • 1
  • 7
  • 29
1
vote
1 answer

How to get "html title with id or more" using preg_match?

I am using preg_match("/\(.*)\<\/title\>/i",$str,$title); to get Exapmle How can I get this using preg_match? Exapmle I am using this to get page title. Maybe, a title can have more than 'id' …
Alper Güven
  • 329
  • 4
  • 15
1
vote
1 answer

Editing vbscript while looping through folder path - PHP

I have a php code that will loop through my file folder and edit my vbscript at the same time. My code is working but not the way I expected. Here are the contents of my Samplefolders: Here is my VB Script Code: Option Explicit Dim oFSO,…
Rukikun
  • 271
  • 3
  • 19
1
vote
4 answers

How to validate letter & number or only letter strings?

I want to match letters and numbers, but not only numbers. So I want to create a preg_match() pattern to filter like this: eg: no123 true 123no true 123456 false letters true
Mr Myo
  • 49
  • 3
  • 10
1
vote
2 answers

Function preg_match() is not returning any value

I'm using preg_match() to match a pattern from a string and parse them further... But it's not returning any value... Actually, I'm using that to fetch the videos by looking after the header information. $url = "http://www.youtube.com/watch?v=" .…
Vijay
  • 5,331
  • 10
  • 54
  • 88