Jump to content

Variable Within Regex


member123

Recommended Posts

I'm trying to use a column of regular expressions in a MySQL table to check for certain phrases in a block of text.

 

I am creating an array with the column of data and then using a foreach loop to try out each row.

 

The regular expressions need to include a variable, because I am using this with another array of words.

 

What syntax do I use with the preg_match function and the column of regex values if I want to include a variable?

Link to comment
https://forums.phpfreaks.com/topic/105692-variable-within-regex/
Share on other sites

	$posregexes = array('/' .$variant . '\\s+(wins)\\s+(series)/',
				'/$variant\\s+(hires)\\s+(player)/'
			);


			foreach($posregexes as $regex) {
			echo $regex . "<br />";
			echo $variant;
				if (preg_match ($regex, $shebang))
    					{
    						$tags[] = 'positive';
    					}
			}
			foreach($negregexes as $regex) {
			echo $regex . "<br />";
			echo $variant;
				if (preg_match ($regex, $shebang))
    					{
    						$tags[] = 'negative';
    					}
			}
	}

 

$variant is itself the output from an outer foreach loop, so its value will be different each time this code is run.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.