0

a form pass a string with wildcard characters (*) to the php page, I need to retrive all the matching words in a text file already on the server.

I think regex is a good way.

here a little piece of code:

$w= 'h**e'; // example of word 

$matches=array();


$filename = 'words.txt'; //txt file

$words= file_get_contents ( $filename); //read file
$w=str_replace ("*", '\w',$w); //replace * with \w

preg_match_all('/'.$w.'/', $words, $matches); // get all matching string


var_dump($matches[0]); //print var

I replace wildcar char * with \w, this way I can find many occurences, for example i found 'hope' and 'hype', but also 'hopeless'. I need only the words with the correct length.

I'm sure my regex is incomplete, but I can't find the right one. For example '/^h\w\we$/' didn't work.

any help? thank you

0 Answers0