member123 Posted May 15, 2008 Share Posted May 15, 2008 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 More sharing options...
redarrow Posted May 15, 2008 Share Posted May 15, 2008 I think you got the method wrong some how? If you want to use a regex agin and agin, you need to create a function for the regex mate... You call the function each time you need to re-use the current regex..... Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-541527 Share on other sites More sharing options...
member123 Posted May 15, 2008 Author Share Posted May 15, 2008 I'm using the preg_match function. I'm feeding it the same regex each time, except the variable embedded in the regex is changing each time. Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-541535 Share on other sites More sharing options...
redarrow Posted May 15, 2008 Share Posted May 15, 2008 please provide your current example........ Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-541536 Share on other sites More sharing options...
member123 Posted May 15, 2008 Author Share Posted May 15, 2008 $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. Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-541552 Share on other sites More sharing options...
member123 Posted May 15, 2008 Author Share Posted May 15, 2008 Anybody know what syntax I'm supposed to use for the embedded variable? Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-542406 Share on other sites More sharing options...
DarkWater Posted May 15, 2008 Share Posted May 15, 2008 It has to be in double quotes so the variable is parsed. Link to comment https://forums.phpfreaks.com/topic/105692-variable-within-regex/#findComment-542419 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.