how do I write this regex expression to use with with preg_match?

php regular expression tester
php regex cheat sheet
php preg_match case insensitive
php preg_match array of patterns
regular expression tutorialspoint
advantage of regular expression in php
php preg_match url pattern

I have a series of keywords I want to match against an item name to determine if it is a labor line item or not. How can I write the pattern regex so it matches them all in a more succinct block of code?

if(preg_match('/(LABOR)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(SERVICES)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Pull and Prep)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Supervision)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Coordination)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Disposal)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Handling)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Inspect)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Re-ship)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Storage)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Management)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Show Form)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Receive)/i', $Item[Name])){        
    $subtotal += $mysubtotal;
}elseif(preg_match('/(Paperwork)/i', $Item[Name])){
    $subtotal += $mysubtotal;

The pipe, |, with preg_replace and its fifth parameter should be all you need.

preg_replace('/LABOR|SERVICES|Pull and Prep|Supervision|Coordination|Disposal|Handling|Inspect|Re-ship|Storage|Management|Show Form|Receive|Paperwork/i', '', $Item['Name'], -1, $subtotal);

You might want to use word boundaries as well so you know that it is an exact match (unless laboring, labors, etc are valid?):

preg_replace('/\b(?:LABOR|SERVICES|Pull and Prep|Supervision|Coordination|Disposal|Handling|Inspect|Re-ship|Storage|Management|Show Form|Receive|Paperwork)\b/i', '', $Item['Name'], -1, $subtotal);

PHP Regular Expressions Tutorial: Preg_match, Preg_split , Regular expressions are powerful pattern matching algorithm that can be performed in a single expression.Regular expressions use arithmetic  PHP regex pattern 3 - preg_match for a PHP URI (GET request) Okay, I just need to do some more preg_match work for a new PHP program I'm working on, and created the following test script for a preg_match pattern to test against a URI as part of testing the data I receive in a GET request.

Just use | (means OR).

So, your cole would look like this:

if(preg_match('/LABOR|SERVICES|Pull and Prep|...|.../i', $Item['Name'])){        
    $subtotal += $mysubtotal;

And, of course, don't forget to quote array keys (thanks to @mario): $Item['Name'].

preg_match - Manual, Using offset is not equivalent to passing substr($subject, $offset) to preg_match() in place of the subject string, because pattern can contain  How to write Regular Expressions? A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.(Wikipedia).

You can use | as others suggested. Or, you can also try for in_array() and match with group 0 of the regex.


$desired_values = ['LABOR','SERVICES','Pull and Prep','Supervision','Coordination','Disposal','Handling','Inspect','Re-ship','Storage','Management','Show Form','Receive','Paperwork'];

$matches = [];

$string = "Pull and Prep";

if(preg_match('/.+/i',$string,$matches) && in_array($matches[0],$desired_values)){
    $subtotal += $mysubtotal;

Getting Started with PHP Regular Expressions, In PHP every regular expression pattern is defined as a string using the Perl format. In Perl, a regular expression pattern is written between forward slashes, such  How do I isolate the text between 2 delimiters on the left and 7 delimiters on the right in Python? python,regex,string,split. You can use python's built-in csv module to do this. j = next(csv.reader([string])); Now j is each item delimited by a , and will include commas if the value is wrapped in ".

If I have a list, A, of regexp's and I want to know if a string contains any of them, I can preg_match:

A[1] +"|"+ A[2] +"|"+ A[3]

Here, | is the "OR" or "Union" character for regular expressions and + is concatenation.

Regular Expressions in PHP, Regular expression syntax includes the use of special characters (do not confuse with the HTML special characters). you'd have to write \. a pattern exists in a string or not using the regular expression and PHP preg_match() function:  Regular expressions in PHP. PHP has built in functions that allow us to work with regular functions. Let’s now look at the commonly used regular expression functions in PHP. preg_match – this function is used to perform a pattern match on a string. It returns true if a match is found and false if a match is not found.

how do I write this regex expression to use with with preg_match , The pipe, | , with preg_replace and its fifth parameter should be all you need. preg_replace('/LABOR|SERVICES|Pull and  Matching a string. In PHP, you can use the preg_match() function to test whether a regular expression matches a specific string. Note that this function stops after the first match, so this is best suited for testing a regular expression more than extracting data. // Lets use a regular expression to match a date string.

PHP - Regular Expressions, They provide the foundation for pattern-matching functionality. Using regular expression you can search a particular string inside a another string, you can  Some times a Hacker use a php file or shell as a image to hack your website. so if you try to use move_uploaded_file() function as in example to allow for users to upload files, you must check if this file contains a bad codes or not so we use this function. preg match

PHP Regex Tutorial, Using Regular Expressions with PHP as a delimiter, for the regex pattern \b\w+\​b , this is the string you would feed to a preg function: '~\b\w+\b~' For instance, consider a movie title written in camel case (perhaps because it was in a file  The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise. If the optional input parameter pattern_array is provided, then pattern_array will contain various sections of the subpatterns contained in the search pattern, if applicable. If this flag is