-1

I create a contact form but users send my spam content in another language (my language is Hebrew).

I try to force send english text that his percent from the total string is more than X percent (for example 10%). Here's my code:

$sent_text = 'שלום, שמי הוא דין. Hello';
$check = similar_text($sent_text, '[A-Za-z]', $percent);
echo 'percent of english is: ' . $check($percent);

I expect the output to be 20% but nothing return.

Amanda
  • 21
  • 4
  • The second string you're passing to ``similar_text()`` is ``'[A-Za-z]'`` but that looks like a regexp. If you're trying to see how many English letters are in the string, you'll need to use something like ``preg_match()``. – kmoser Dec 23 '18 at 05:51

3 Answers3

1

You can't judge whether a text is english by usage of the alphabet. The best way might be to use an english dictionary file and see if any of the whole words appear in the dictionary.

Evert
  • 93,428
  • 18
  • 118
  • 189
0

If you simply want to determine what percentage of the string $sent_text contains the letters A-Z (case-insensitive):

100 * strlen( preg_replace( '/[^a-z]/i', '', $sent_text ) ) / strlen( $sent_text );
kmoser
  • 8,780
  • 3
  • 24
  • 40
0

If you are trying to calculate English characters:

$sent_text = 'שלום, שמי הוא דין. Hello';
$english_text = preg_replace('/[^a-zA-Z]/', '', $sent_text);
$check = similar_text($sent_text, $english_text, $percent);
echo 'percent of english is: ' . $percent;

Please look at following post related to similar_text()

How does similar_text work?

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • Oh, I forgot to use preg function. you help me so much, thanks! – Amanda Dec 23 '18 at 06:00
  • FYI that is not "English", English is a language, not an alphabet. Latin characters are used in nearly every European language. – J.A.P Dec 23 '18 at 07:35
  • Yes but we need to know what actually OP wants to achieve and what are the problems in code. – Naveed Dec 23 '18 at 07:39