-1

I have a website where user can add reviews. Now Issue is some user add very lengthy strings and I'm unable to stop them to do that, like a user added a review:

aatdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd 

Another user put this review:

ggvddghjbxvjfuihdfjkljgghkkollkkkkkkļfghjjkjjjjjjjjjjhjbjjxxfdfhgsxvvbdsxvvgssxcxxźvgggfggffccccffghjjjjjjjhfdsxvhjnbdddhugsdhjnnhhjjjjnnjxxssfghjiikjkmjjhgcxsdghjkkkk

Here is code:

if (isset($_POST['addReview'])) {
    $summary = isset($_POST['summary']) ? trim($_POST['summary']) : '';

How I can check input string length and make sure there are no reviews like above and input is only words?

Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
  • Possible duplicate of [Check String Length In PHP](http://stackoverflow.com/questions/5563152/check-string-length-in-php) – Epodax Jul 26 '16 at 08:12
  • That answer is not helpful for me. – Muhammad Shahzad Jul 26 '16 at 08:14
  • @Epodax Not really though, this question combines checking string length and checking for the existence of a space. – roberto06 Jul 26 '16 at 08:15
  • So, don't want a limit on how many words / length but simply that people don't post gibberish? That's gonna be hard to check, if it's because of the cosmetic then I believe some one all ready posted a answer to that. – Epodax Jul 26 '16 at 08:20
  • @Epodax so you mean answer is not possible!!! :))) I give a try to that answers but not works for me that's why I come here to ask. – Muhammad Shahzad Jul 26 '16 at 08:28
  • ..uh what? No, I said it's gonna be hard, not impossible, there's a few things that are actually impossible to code. – Epodax Jul 26 '16 at 08:38

6 Answers6

2
$post = $_POST['review'];
$postArray = explode(' ' , $post);
$flag=0;
foreach($postArray as $value)
{
    if(strlen($value)>45)  // the largest word has count 45 characters , you can change the count
         $flag = 1;
}

 if($flag)
    echo "word not allowed";

As the largest word is of 45 characters , so you can put a check like above , but you can change the word length as their is less probability of user putting that word.

Master Yoda
  • 531
  • 8
  • 22
  • I got issue!! This is my input string: `hi this is review for testing` and I got error message: "word not allowed", why this because in input string no words have length more than 45 characters. – Muhammad Shahzad Jul 27 '16 at 08:04
  • `45) // the largest word has count 45 characters , you can change the count $flag = 1; } if($flag) echo "word not allowed"; else echo "words are ok"; ?>` please check this , i dodn't got the error – Master Yoda Jul 27 '16 at 08:52
  • In this case it is working fine but in case of actual post submission it is not working. – Muhammad Shahzad Jul 27 '16 at 09:06
  • please paste the whole code so that i can see it . https://ideone.com/ , add the code here and share it will me so i can see – Master Yoda Jul 27 '16 at 09:39
1

A better solution would be to use

word-wrap: break-word;

It will break the string when it gets too long

1

You should use the following functions :

In the end, you should have something like this :

if (strlen($string) > $limit && stristr($string, ' ') === false) {
    // Don't store the string
}
roberto06
  • 3,844
  • 1
  • 18
  • 29
0

In php you can check or count total word to make a minimum sentence and also check the length

$string="Hello world!";
$count= str_word_count($string);
$arr = explode(' ',trim($string));
$len=strlen($arr[0]);

In Css you can view to use

word-wrap:break-word;

suman das
  • 357
  • 1
  • 12
0

Instead of manipulating your data, you can just wrap it on the UI layer using CSS. Use:

word-wrap: break-word;

For more info, see this http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-wrap

-1

Well I don't know if you are using PHP, but the solution is quite simple: Let's say you want a review to have maximum 100 length:

if (strlen($review)<100) {
 // post the review
}
else {
  die("Sorry the input is too large.");
}

Another way of doing it, but it`s not safe:

<input type="text" name="review" maxlength="100">

Hope it helps.

Mecanik
  • 1,539
  • 1
  • 20
  • 50