xymcrush17 Posted May 11, 2015 Share Posted May 11, 2015 I have a group of letter that consists letters like $words= 'estroaroint'; that can be arranged to be some words in my list $file = 'dictionary.txt' Here my expected result that on my words list: STORE REST TRAIN RESTORATION ...etc I searched on google and found like : $contents = file_get_contents($file); $pattern = preg_quote($words, '/'); $pattern = "/^.*$pattern.*\$/m"; if(preg_match_all($pattern, $contents, $matches)){ echo "Found matches:\n"; echo implode("\n", $matches[0]); echo strlen($matches); } else{ echo "No matches found"; } But that's not like as my expectation Thanks in advance Warm Regard Link to comment https://forums.phpfreaks.com/topic/296194-is-right-using-preg_match-to-search-word-that-contain-specific-letters/ Share on other sites More sharing options...
QuickOldCar Posted May 11, 2015 Share Posted May 11, 2015 Explain a little more what you want to accomplish, a snippet of the file and expected results. Do you want to check to see if the word exists in a line of the file? For the record it's probably better to use a database for this, searching large text files is not friendly and a resource hog. Link to comment https://forums.phpfreaks.com/topic/296194-is-right-using-preg_match-to-search-word-that-contain-specific-letters/#findComment-1511381 Share on other sites More sharing options...
xymcrush17 Posted May 11, 2015 Author Share Posted May 11, 2015 Yes, I do. I want to check word that exist in a line of file but it contains letter 'estroaroint'. for example we can find in the line of file like words STORE REST TRAIN EAST RESTORATION ..etc And Thanks for suggestion using Database.. Could you help what sql command to search it what I expect,please? Link to comment https://forums.phpfreaks.com/topic/296194-is-right-using-preg_match-to-search-word-that-contain-specific-letters/#findComment-1511385 Share on other sites More sharing options...
Ch0cu3r Posted May 11, 2015 Share Posted May 11, 2015 Seems to me you are trying to write a anagram solver? To find possible words from the letters provided Yes one way would be to use preg match ( anagram letters in a character class wrapped in a word boundary ) to see if the letters used in the word are contained in the anagram. But this will only get you as far as finding possible words from the anagram. You will now need to check to make sure each letter used in the word does not exceed how may times it was used in the anagram, eg the if the letter t is used twice in the anagram then the word cannot use the the letter t more then twice. You can use count_chars to help you there. Link to comment https://forums.phpfreaks.com/topic/296194-is-right-using-preg_match-to-search-word-that-contain-specific-letters/#findComment-1511391 Share on other sites More sharing options...
Barand Posted May 11, 2015 Share Posted May 11, 2015 One way to speed up your search if you use a database is to store the word with its "word root" (ie the letters in the word sorted into alphabetic order). All anagrams have the same root. For example, if you have found EAST you can quickly find others word | root -----+------ east | aest eats | aest seat | aest sate | aest tase | aest Link to comment https://forums.phpfreaks.com/topic/296194-is-right-using-preg_match-to-search-word-that-contain-specific-letters/#findComment-1511431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.