I have a site where users can put in a description about themselves.
Most users write something appropriate but some just copy/paste the same text a number of times (to
// 3 examples of how you might detect repeating user input
// use preg_match
// pattern to match agains
$pattern = '/^text goes here$/';
// the user input
$input = 'text goes here';
// check if its match
$repeats = preg_match($pattern, $input);
if ($repeats) {
var_dump($repeats);
} else {
// do something else
}
// use strpos
$string = 'text goes here';
$input = 'text goes here';
$repeats = strpos($string, $input);
if ($repeats !== false) {
# code...
var_dump($repeats);
} else {
// do something else
}
// or you could do something like:
function repeatingWords($str)
{
$words = explode(' ', trim($str)); //Trim to prevent any extra blank
if (count(array_unique($words)) == count($words)) {
return true; //Same amount of words
}
return false;
}
$string = 'text goes here. text goes here. ';
if (repeatingWords($string)) {
var_dump($string);
} else {
// do something else
}