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

How to remove specific content between two php tags from php file

I am trying to remove content between two php tags from a php file I tried many methods but not succeed please help here is an example code. I am getting garbage content in all of my php files due to some virus attack: Garbage Content in…
Stackfull
  • 93
  • 9
1
vote
3 answers

Regex: how to stop match on certain words that cannot exists

The title is so confusing, I admit it - if you want to change it you are welcome. I'm working on PHP and preg_match function. The problem is this string (just an example) ON DELETE SET NULL ON UPDATE CASCADE ON DELETE CASCADE ON UPDATE CASCADE…
apelliciari
  • 8,241
  • 9
  • 57
  • 92
1
vote
2 answers

regex for php to find all self-closing tags

I've got a system that uses a DomDocumentFragment which is created based on markup from a database or another area of the system (i.e. other XHTML code). One such tag that may be included is:
Before the string is added to…
Marty
  • 11
  • 2
1
vote
1 answer

Regex pattern works on string but not on loaded file content

I want to extract words between ";" and ":" from an XML file, for example the word " Index" here bla bla bla ; Index : bla bla the file is loaded by its URL using file_get_contents $output =…
lady_OC
  • 417
  • 1
  • 5
  • 20
1
vote
3 answers

PHP Regular Expression

I am trying to write a regular expression to validate file names on a file system. Examples of valid file names are tapa_newcougar_org.png tapa_lamborghini-talk_com.png tapa_clubfrontier_org.png The logic behind valid is the image starts with…
dnaluz
  • 135
  • 1
  • 10
1
vote
1 answer

How to replace substrings using randomly selected array elements?

Just a comment: In a previous question, I need to change the values, from column 0 to 1 or 1 to 0, depending on any value being parsed in the $myVar variable, the solution appended by a user here on stackoverflow was ideal: $myWords=array( …
1
vote
2 answers

How to validate zero or more image names, allowing spaces, separated by commas or empty

I need to validate images with regular expressions, with the following conditions: $string may be empty I can receive a single image with this format, for example: 2017-06-01-03-55-00-5930782c5c0b1.jpg I can get several image names separated by…
Learning and sharing
  • 1,378
  • 3
  • 25
  • 45
1
vote
2 answers

Using arrays in PHP, how to always change the key1 by key2 or change key2 by key1?

My previous code was a rand, which has nothing to do with my current intent, which is to replace whenever I find a value, by the other value of the array. $myWords=array( array('funny','sad'), array('fast','slow'), …
1
vote
1 answer

preg_match only letters, numbers and spaces (including umlauts and similar)

I know there are a lot of these questions here on StackOverflow but i couldn't find exactly what i'am searching for ... I need a regex that allows letters (including umlauts and others like öäßè), numbers and white space. So no special characters…
GDY
  • 2,872
  • 1
  • 24
  • 44
1
vote
2 answers

Why so many backslashes in this preg_match for resolving namespaces?

A book I'm reading has this code but cant understand why so many backslashes in preg_match. The first \ might mean to match literal backslash but why two more in the expression? This is code to "provide namespace support" with autoload: // listing…
user7558369
1
vote
3 answers

PHP preg_match matching text between multiple occurrance of same tags

How to match text $line = "study of 557 adults suffering from sleep"; from $content = "The International Journal of Nursing published a study of 557 adults suffering
from sleep disorders. In this study, music was played when…
1
vote
4 answers

Extract Characters Before And After Middle Symbol regex and rest string

I have a file name like this 1x5 Girl In The Flower Dress.mkv It means Season 1 Episode 5 of In The Flower Dress 2x6.English,.Fitz.or.Percy.avi It means Season 2 Episode 6 of English, Fitz or Percy How to extract Season number ,Episode number…
Try Again
  • 87
  • 8
1
vote
2 answers

php preg_match() not working, find a var

header.php file
user7700973
1
vote
1 answer

passing parenthesis in Preg_match() - php

I'm not able to match parenthesis for below code rest of the conditions are working fine. if (!preg_match(!preg_match("/^[a-zA-Z0-9 -,._\/\))]{10,50}$/",$phone))) { $_SESSION['nameErr'] .= "
  • Phone should…
  • Jasshh Andrews
    • 175
    • 1
    • 15
    1
    vote
    1 answer

    PHP: Can preg_match include unmatched groups?

    Can the preg_match() function include groups it did not find in the matches array? Here is the pattern I'm using: /^([0-9]+)(.[0-9]+)?\s?([^iIbB])?([iI])?([bB])?$/ What I'm trying to is parse an human readable size into bytes. This pattern fits my…
    Adambean
    • 1,096
    • 1
    • 9
    • 18