imperium2335 Posted December 11, 2009 Share Posted December 11, 2009 Hi, How would I make a regular expression that checks if the word "cheese" is preceded by a number using preg_match? e.g. 6 cheese (including the space) Regards, Tom. Quote Link to comment https://forums.phpfreaks.com/topic/184758-regular-expression/ Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 Firstly there's a board for Regular Expressions, please try to keep posts in the right sections (I moved this particular one for you). That's going to be a fairly easy task, a lot depends on exactly what you wish to do. This will detect if a string has a sub-string of the form 'number-space-cheese'... $input = "john has 6 cheese slices"; if(preg_match('#[0-9]+ cheese\b#', $input)) { echo "Yes there is a number followed by a space,followed by the word cheese in the variable input"; } else { echo "No there isn't."; } Quote Link to comment https://forums.phpfreaks.com/topic/184758-regular-expression/#findComment-975348 Share on other sites More sharing options...
nafetski Posted December 20, 2009 Share Posted December 20, 2009 This might work. Should make the space character optional as well if(preg_match('/\d+\s?cheese/', $input)) { } Quote Link to comment https://forums.phpfreaks.com/topic/184758-regular-expression/#findComment-980803 Share on other sites More sharing options...
salathe Posted December 20, 2009 Share Posted December 20, 2009 Will the entire string be "6 cheese" or is that only part of a larger one? Quote Link to comment https://forums.phpfreaks.com/topic/184758-regular-expression/#findComment-980897 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.