Jump to content

Is right using preg_match to search word that contain specific letters?


Recommended Posts

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:

  • REST
  • ...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);
   echo "No matches found";

But that's not like as my expectation


Thanks in advance

Warm Regard

Edited by xymcrush17
Link to comment
Share on other sites

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
Share on other sites

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

  • REST
  • EAST
  • ..etc

And Thanks for suggestion using Database..

Could you help what sql command to search it what I expect,please?

Link to comment
Share on other sites

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.

Edited by Ch0cu3r
Link to comment
Share on other sites

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
Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.