I have issue in php preg_match..
I tried using preg_match to check this string..
$txt = 'dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.catdog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog';
echo count(explode('.', $txt)) . "<br>";
echo strlen($txt) , "<br>";
if(preg_match("/[a-z]+(?:([.][a-z]+)*)/i", $txt)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
But I think there is limit in preg_match.. the total count after I explode the dot is 1638, more than 1638, it returns not match.
But when I tried using phpliveregex or regex 101 it can match the regex..
so here is the complete explanation..
I created a program for checking a format..
continue from this issue Javascript string match specific regex
User can input anything in textarea, as long as it's correct then it will retrive the data, but when user input wrong format, I should remove the wrong format by using preg_replace and retrive a correct format, when none of format is correct, then it will return error message..
this not 1 format only, there are about 10 formats I should check..
so this is not just a simple to explode the ., +, * or use cytpe_alpha().
The conclusion is the program or the product owner does not care what user input into the text area, they can input 10 formats into the textarea.. as long as I can check and passed the format, then it all be good..
example of wrong input that I should fix into correct format..
150-50-30----20=50+dog......cat.......cow.....chicken,,,,.,.,pencil,
dog,,,.,cat.,.,.chicken......50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+*4*8=32
so after I correct the format, it will be like this..
150-50-30-20=50+dog.cat.cow.chicken.pencil.dog.cat.chicken.50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+4*8=32
can anyone help with this issue?
It appears that you have reached the memory limit on "capturing". If you simply don't capture any substrings, you'll be just fine.
Code: (Demo)
$txt = 'dog.cat' . str_repeat('.dog.cat', 815);
for ($i = 0; $i < 5; ++$i) {
$txt .= '.dog.cat';
echo count(explode('.', $txt)) . "\n";
echo strlen($txt) . "\n";
if (preg_match("/[a-z]+(?:\.[a-z]+)*/i", $txt, $match)) {
echo "A match was found." . strlen($match[0]);
} else {
echo "A match was not found.";
}
echo "\n---\n";
}
Output:
1634
6535
A match was found.6535
---
1636
6543
A match was found.6543
---
1638
6551
A match was found.6551
---
1640
6559
A match was found.6559
---
1642
6567
A match was found.6567
---
About the error: https://3v4l.org/RjfiN#v7.2.0
You are getting error constant 6.
PREG_JIT_STACKLIMIT_ERROR
preg_last_error_msg() isn't available until PHP8.
I see some good answers at: PHP PREG_JIT_STACKLIMIT_ERROR - inefficient regex
If preg_match() is proving to be unsuitable/miserable for your requirements, then stop using it. There are other available tools to determine if a string is strictly comprised of dot-separated alphabetical substrings.
function isDotSeparatedAlphabetical($string) {
foreach (explode('.', $string) as $word) {
if (!ctype_alpha($word)) {
return false;
}
}
return true;
}
$txt = 'dog.cat' . str_repeat('.dog.cat', 815);
for ($i = 0; $i < 5; ++$i) {
$txt .= '.dog.cat';
var_export(['length' => strlen($txt), 'verdict' => isDotSeparatedAlphabetical($txt)]);
echo "\n---\n";
}
Related
I need to validate an input with the following pattern in php:
(4 digits)/(previous number + 1)
The first digit needs to be a 1 or a 2.
As an Example:
2018/2019 //valid
2000/2010 //invalid
1999/2000 //valid
2020/2019 //invalid
I've managed to get that both groups are equal, but I can't figure out how to increment the back reference by one...
/([1-2]\d{3})\/(\1)/
The (\1) needs to be incremented by one.
Is this possible with regex?
You want to first validate the string is in <BOS><YEAR>/<YEAR><EOS> format and then check if the second year is an incremented first year.
You may use ^([12]\d{3})/([12]\d{3})\z regex (see demo) to match and validate the string, and also capture the first year into Group 1 and the second year into Group 2.
Use
$strs = ['2018/2019', '2000/2010','1999/2000','2020/2019', 'aaaa'];
foreach ($strs as $s) {
if (preg_match('~^([12]\d{3})/([12]\d{3})\z~', $s, $m)) {
if ((int)$m[2] - (int)$m[1] == 1) {
echo $s . " is valid!" . PHP_EOL;
} else {
echo $s . " is NOT valid!" . PHP_EOL;
}
} else {
echo $s . " is NOT valid!" . PHP_EOL;
}
}
See the PHP demo, output:
2018/2019 is valid!
2000/2010 is NOT valid!
1999/2000 is valid!
2020/2019 is NOT valid!
aaaa is NOT valid!
I have a string formed up by numbers and sometimes by letters.
Example AF-1234 or 345ww.
I have to get the numeric part and increment it by one.
how can I do that? maybe with regex?
You can use preg_replace_callback as:
function inc($matches) {
return ++$matches[1];
}
$input = preg_replace_callback("|(\d+)|", "inc", $input);
Basically you match the numeric part of the string using the regex \d+ and replace it with the value returned by the callback function which returns the incremented value.
Ideone link
Alternatively this can be done using preg_replace() with the e modifier as:
$input = preg_replace("|(\d+)|e", "$1+1", $input);
Ideone link
If the string ends with numeric characters it is this simple...
$str = 'AF-1234';
echo $str++; //AF-1235
That works the same way with '345ww' though the result may not be what you expect.
$str = '345ww';
echo $str++; //345wx
#tampe125
This example is probably the best method for your needs if incrementing string that end with numbers.
$str = 'XXX-342';
echo $str++; //XXX-343
Here is an example that worked for me by doing a pre increment on the value
$admNo = HF0001;
$newAdmNo = ++$admNo;
The above code will output HF0002
If you are dealing with strings that have multiple number parts then it's not so easy to solve with regex, since you might have numbers overflowing from one numeric part to another.
For example if you have a number INV00-10-99 which should increment to INV00-11-00.
I ended up with the following:
for ($i = strlen($string) - 1; $i >= 0; $i--) {
if (is_numeric($string[$i])) {
$most_significant_number = $i;
if ($string[$i] < 9) {
$string[$i] = $string[$i] + 1;
break;
}
// The number was a 9, set it to zero and continue.
$string[$i] = 0;
}
}
// If the most significant number was set to a zero it has overflowed so we
// need to prefix it with a '1'.
if ($string[$most_significant_number] === '0') {
$string = substr_replace($string, '1', $most_significant_number, 0);
}
Here's some Python code that does what you ask. Not too great on my PHP, but I'll see if I can convert it for you.
>>> import re
>>> match = re.match(r'(\D*)(\d+)(\D*)', 'AF-1234')
>>> match.group(1) + str(int(match.group(2))+1) + match.group(3)
'AF-1235'
This is similar to the answer above, but contains the code inline and does a full check for the last character.
function replace_title($title) {
$pattern = '/(\d+)(?!.*\d)+/';
return preg_replace_callback($pattern, function($m) { return ++$m[0]; }, $title);
}
echo replace_title('test 123'); // test 124
echo replace_title('test 12 3'); // test 12 4
echo replace_title('test 123 - 2'); // test 123 - 3
echo replace_title('test 123 - 3 - 5'); // test 123 - 3 - 6
echo replace_title('123test'); // 124test
I have a string formed up by numbers and sometimes by letters.
Example AF-1234 or 345ww.
I have to get the numeric part and increment it by one.
how can I do that? maybe with regex?
You can use preg_replace_callback as:
function inc($matches) {
return ++$matches[1];
}
$input = preg_replace_callback("|(\d+)|", "inc", $input);
Basically you match the numeric part of the string using the regex \d+ and replace it with the value returned by the callback function which returns the incremented value.
Ideone link
Alternatively this can be done using preg_replace() with the e modifier as:
$input = preg_replace("|(\d+)|e", "$1+1", $input);
Ideone link
If the string ends with numeric characters it is this simple...
$str = 'AF-1234';
echo $str++; //AF-1235
That works the same way with '345ww' though the result may not be what you expect.
$str = '345ww';
echo $str++; //345wx
#tampe125
This example is probably the best method for your needs if incrementing string that end with numbers.
$str = 'XXX-342';
echo $str++; //XXX-343
Here is an example that worked for me by doing a pre increment on the value
$admNo = HF0001;
$newAdmNo = ++$admNo;
The above code will output HF0002
If you are dealing with strings that have multiple number parts then it's not so easy to solve with regex, since you might have numbers overflowing from one numeric part to another.
For example if you have a number INV00-10-99 which should increment to INV00-11-00.
I ended up with the following:
for ($i = strlen($string) - 1; $i >= 0; $i--) {
if (is_numeric($string[$i])) {
$most_significant_number = $i;
if ($string[$i] < 9) {
$string[$i] = $string[$i] + 1;
break;
}
// The number was a 9, set it to zero and continue.
$string[$i] = 0;
}
}
// If the most significant number was set to a zero it has overflowed so we
// need to prefix it with a '1'.
if ($string[$most_significant_number] === '0') {
$string = substr_replace($string, '1', $most_significant_number, 0);
}
Here's some Python code that does what you ask. Not too great on my PHP, but I'll see if I can convert it for you.
>>> import re
>>> match = re.match(r'(\D*)(\d+)(\D*)', 'AF-1234')
>>> match.group(1) + str(int(match.group(2))+1) + match.group(3)
'AF-1235'
This is similar to the answer above, but contains the code inline and does a full check for the last character.
function replace_title($title) {
$pattern = '/(\d+)(?!.*\d)+/';
return preg_replace_callback($pattern, function($m) { return ++$m[0]; }, $title);
}
echo replace_title('test 123'); // test 124
echo replace_title('test 12 3'); // test 12 4
echo replace_title('test 123 - 2'); // test 123 - 3
echo replace_title('test 123 - 3 - 5'); // test 123 - 3 - 6
echo replace_title('123test'); // 124test
$word = "superman";
I would like to be able to randomly choose 2 letters from what ever word is in $word and build a fill in the blank box for me to be able to answer
example some how random picks the p and the a
su_erm_n comes up so I would fill in the first box with p and the second one with a I guess with a form
$wordfinished = "su" . $fillinblank1 . "erm" . $fillinblank2 . "n";
if ($wordfinshed == $word) {
echo "congrats";
}
else{
echo "try again";
}
I am just learning php I have some things that I have done that are very complicated but was having a hard time with the random stuff any help would help me learn this
You can use PHP's rand() function to select a random number from 0 to the length of the word you are modifying and change the character at that index to something else
For example:
$str = "superman";
$str[rand(0, strlen($str)-1)] = "_";
Assuming that the rand() function output a value of 3 for example, we'd end up with this output:
sup_rman
We can put this in a function that can be called more than once in order to make more than one blank space in the word:
function addBlank($str){
do{
$index = rand(0, strlen($str)-1);
$tmp = $str[$index];
$str[$index] = "_";
} while($tmp =! "_");
return $str;
}
This function accepts a string and after each call will replace a letter in the string with _. Each call will result in one more blank space. The variable $tmp holds the value of the string at the randomly chosen index and checks that it wasn't already a blank space, and if it was, it picks another index to try to replace.
So to put this in practice, we can call the above function multiple times and store the result back into a variable, here is example output:
$str = addBlank("superman");
//the value of $str is now sup_rman
$str = addBlank($str)
//the value of $str is now sup_r_an
$word = "superman";
$size = strlen($word) -1;
$fillinblank1 = $word[rand(0,$size)];
$fillinblank2 = $word[rand(0,$size)];
$wordfinished = "su" . $fillinblank1 . "erm" . $fillinblank2 . "n";
echo $wordfinished;
Output:
sueermun
Demo
http://ideone.com/Z66xsI
Just like the title of this post says, I would to be able to check if every letter of a word is found in another word. So far these are the lines of codes that I was able to come up with:
<?php
$DBword = $_POST['DBword'];
$inputWords = $_POST['inputWords'];
$inputCount = str_word_count($inputWords,1);
echo "<b>THE WORD:</b>"."<br/>".$DBword."<br/><br/>";
echo "<b>WORDS ENTERED:</b><br/>";
foreach($inputCount as $outputWords)
{
echo $outputWords."<br/>";
}
foreach($inputCount as $countWords)
{
for($i=0; $i<strlen($countWords); $i++)
{$count = strpos( "$DBword", $countWords[$i]);}
if($count === false)
{
$score++;
}
}
echo "<b><br/>TOTAL SCORE: </b>";
echo $score;
?>
My point in having the foreach with the $outputWords is to just output the letters entered.
As for the other foreach that has $countWords, I am using it to really check if all letters in the word entered are found in the $DBword. I am using the for loop to check every letter.
So far, I am not getting the output that I want and I just ran out of ideas. Any ideas please?
function contains_letters($word1, $word2) {
for ($i = 0; $i < strlen($word1); $i++)
if (strpos($word2, $word1{$i}) === false)
return false;
return true;
}
//example usage
if (contains_letters($_POST['inputWords'], $_POST['DBword']))
echo "All the letters were found.";
If this check should be case-insensitive (i.e. 'A' counts as a usage of 'a'), change strpos to stripos.
Since you are overwriting $count in the for loop for each letter in $countWords, $count will contain the position of the last letter of $countWord only. Also, I am not sure why you increase score when the letter wasn't found.
In any case, you are making your life more difficult than necessary.
PHP has a function for counting chars in a string:
return count_chars($dbWord, 3) === count_chars($inputWord, 3);
will return true if the same letters are found in both strings.
Example to find all the words having exactly the same letters:
$dbWord = count_chars('foobar', 3);
$inputWords = 'barf boo oof raboof boarfo xyz';
print_r(
array_filter(
str_word_count($inputWords, 1),
function($inputWord) use ($dbWord) {
return count_chars($inputWord, 3) === $dbWord;
}
)
);
will output "raboof" and "boarfo" only.