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
1 answer

(php) how to use preg_replace function to replace all chars except numbers and css units?

I need a preg_replace() expression that removes all characters (letters & punctuations) except numbers and css units. But i need it to match the exact units not letters in it. For example I wrote this expression: $check = preg_replace(…
Eren Süleymanoğlu
  • 1,204
  • 3
  • 18
  • 26
1
vote
2 answers

PHP clear up Array preg_grep

I have currently a problem with arrays in php, i requested all
  • tags from an html site with: $dochtml = new DOMDocument(); $dochtml -> loadHTMLFile("my.html"); /*get all li’s*/ $lis = $dochtml -> getElementsByTagName('li'); My Html…
  • Tom Junaik
    • 13
    • 2
    1
    vote
    2 answers

    PHP regex format for address

    I have used the given regex format for address $address='2/12, 1st street, ch-90'; (preg_match('/^[a-zA-Z0-9\s #,-.]+$/i', $address)) pls suggest me to add the forward slash in pregmatch which matches the above address. Thanks
    pabha
    • 35
    • 9
    1
    vote
    2 answers

    pregmatch not seeing http://

    I need to compare 3 different URLs, in order to that i need to make them identical like example.com I've created a preg_match(). So far i've accomplished to make the urls to example.com when the url looks like : http://www.example.com,…
    1
    vote
    3 answers

    Regex to block multiple items

    I have a form on a site that is collecting user details. There was a fool submitting the form with a name like "Barneycok" from different IP addresses, so I learned how to block that name from going through on the form. I learned a little regex,…
    Jaime
    • 21
    • 6
    1
    vote
    3 answers

    PHP - Preg match with multiple conditions

    I have the following the following string: "#(admin/pages|admin/pages/add|admin/pages/[0-9]+)#" And this string to compare it to: "admin/pages/1" What I need is to return "admin/pages/1" when comparing the 2 strings using preg_match(). I have the…
    Sefa Yavuz
    • 33
    • 1
    • 3
    1
    vote
    1 answer

    PHP - preg_match - assign arbitrary value to a matched element

    assuming we have this regex: preg_match('/\b(xbox|xbox360|360|pc|ps3|wii)\b/i' , $string, $matches); now, whenever the regex match for ex. one of the three xbox methods (xbox|xbox360|360), the $matches, should return just XBOX is this possible…
    Julie Rokk
    • 560
    • 1
    • 7
    • 17
    1
    vote
    2 answers

    PHP preg_match result

    This PHP function working just fine: if (preg_match ("/<(\S+@\S+\w)>.*\n?.*55[0-9]/i",$body,$match)) { echo "found!"; } in This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or…
    Sergio
    • 1,207
    • 7
    • 28
    • 42
    1
    vote
    1 answer

    PHP - searching a body of text with an array of needles, returning the keys of all matches

    Looking to search a body of text and return the keys of any of the array elements that have been found within the text. I currently have the below which works but only returns True on the first element found. $needles = [1 => 'shed', 5 => 'charge',…
    Kieran Headley
    • 647
    • 1
    • 7
    • 21
    1
    vote
    1 answer

    Php preg_match not working with óá

    Hey my routing script isnt working with special chars and i dont have any idea anymore why or how to fix it. My Url: /test/x/hállò/123 My Router added URL: /test/:varx/:variableZ/123 $route['url'] ="/test/x/:name/123"; $reqMet =…
    Juan
    • 65
    • 1
    • 10
    1
    vote
    1 answer

    How to match all keywords after slash using preg_match and Regex?

    here are my possible matching cases /aaa /aaa/ /aaa/bbb /aaa/bbb/ /aaa/bbb/ccc /aaa/bbb/ccc/ I'm trying to catch aaa, bbb and ccc if available I know or sure aaa is there, so the regex can be /(aaa)/([^/]*)... I tried a few other scenarios to catch…
    Patrioticcow
    • 26,422
    • 75
    • 217
    • 337
    1
    vote
    1 answer

    preg_match and escaping when inside double quotes

    Complete PHP newbie here trying to get to grips with literals and escapes, so apologies in advance that this is so basic. I've read lots of questions on preg_match but none seem to address this. I have an old form which contains instances of the…
    5tevooo
    • 29
    • 2
    1
    vote
    1 answer

    preg_match_all - get part between pattern and string

    In this example i have large string with many defined elements in it part of example string here. In this example i get matches from example file (Starting from first(~32) tilde, to ~120 3), which sloud be correct in my regex, but i need update…
    Artis
    • 35
    • 4
    1
    vote
    1 answer

    preg_match to array. PHP

    Calling all the PHP helpers out there. So basically I would like to give the function preg_match a variable that can contain a couple thousand lines of code) and have it search using a wildcard + strings either side of the widlcard. For example I…
    shane
    • 13
    • 1
    • 3
    1
    vote
    2 answers

    Yii2 rules for validating percentage like 65.45

    I know this is very simple and repeated question, But i tried almost all but didnt get any solution. I want to validate a percentage of student like. Eg: 65.45.. The user has to enter value like this only otherwise it should show error. I have given…
    Salman Riyaz
    • 808
    • 2
    • 14
    • 37