lindellfoth Posted July 16, 2011 Share Posted July 16, 2011 Hi I need to find all instances of a word in some text that is either in title case or uppercase but ignoring all lowercase instances. The function is used to locate surnames within the text. The problem is that some surnames eg "Law" are the same as common ordinary words so I need to find "Law" and "LAW" but not "law", so I can reduce the number of irrelevant results. My current code uses preg_match_all in case insensitive mode, but if I simply make it case sensitive then the user will only find "Law" or "LAW", depending on which version they entered. I think there is a function or regular expression to change the first letter of a word to uppercase, but I don't want to change it, just locate instances already existing in the text. Currently the code is: $pattern='/.{0,'.$prechars.'}\b'.$surname.'\b.{0,'.$endchars.'}/is'; preg_match_all($pattern, $page_text, $match); Anyone out there got any ideas? All help much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/ Share on other sites More sharing options...
AbraCadaver Posted July 16, 2011 Share Posted July 16, 2011 Something like this should work: '/\b[A-Z][A-za-z]+\b/' Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/#findComment-1243526 Share on other sites More sharing options...
lindellfoth Posted July 16, 2011 Author Share Posted July 16, 2011 Thanks for the suggestion, but won't that match any uppercase or title case word? I need to match a specific word, input by the user. So if the user inputs the name "Law" for example, I need to find all instances of that particular word as title case ie "Law" or uppercase ie "LAW", but not instances that are all lowercase ie "law". Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/#findComment-1243530 Share on other sites More sharing options...
PFMaBiSmAd Posted July 16, 2011 Share Posted July 16, 2011 I would just make the search term exactly what you are looking for - $surname = ucfirst($surname) .'|'. strtoupper($surname); $pattern="/\b($surname)\b/s"; For your example, $pattern would be: /\b(Law|LAW)\b/s Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/#findComment-1243539 Share on other sites More sharing options...
lindellfoth Posted July 16, 2011 Author Share Posted July 16, 2011 That's brilliant, just what I need. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/#findComment-1243550 Share on other sites More sharing options...
AbraCadaver Posted July 16, 2011 Share Posted July 16, 2011 Thanks for the suggestion, but won't that match any uppercase or title case word? I need to match a specific word, input by the user. So if the user inputs the name "Law" for example, I need to find all instances of that particular word as title case ie "Law" or uppercase ie "LAW", but not instances that are all lowercase ie "law". Yeah sorry. Didn't read closely enough. Quote Link to comment https://forums.phpfreaks.com/topic/242132-match-title-case-or-uppercase-instances-only-of-a-word/#findComment-1243577 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.