Php-mysql Course

The cleanLongWords() function presented in this page can be used to clean text with multiple consecutive characters and long words in PHP. Useful to clean texts that users add in input text fields or textarea.
By default, the function allows maximum 2 consecutive repetitions of characters in words, but not affects the numbers, and split the word to maximum 23 characters. See the comments in code, and example below.

Code of the cleanLongWords() function

- Click to select it.
function cleanLongWords($text, $nrchr=2, $maxword=23) {
// function to clean text with multiple consecutive characters and very long words ( )
// $nrchr = allowed number of consecutive character
// $maxword = maximum word length
  $paterns = array();
  $chr = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l',';','z','x','c','v','b','n','m',',','!','@','#','%','&','_','=',':','"','`','~',';','â','á','é','í','ó','ú','ý','ø','č','ć','đ','š','ž','ā','ä','ǟ','ḑ','ē','ī','ļ','ņ','ō','ȯ','ȱ','õ','ȭ','ŗ','š','ț','ū','ž','ş','î','ă',"'",'$','\^','\*','\(','\)','\{','\}','\|','\?','\.','\[','\]','\/','\\\\','\>','\<');

  // uncomment next line if you want to clean consecutive numbers too
  ///  $chr = array_merge($chr, array('1','2','3','4','5','6','7','8','9','0'));
  $n_chr = count($chr);
  for($i=0; $i<$n_chr; $i++) {$paterns[$i] = '/(['. $chr[$i] .']{'. $nrchr .',}){2,}/i'; }
  $text = preg_replace($paterns, '$1', $text);

  // if $maxword > 0, split the word to specified number of characters
  return ($maxword > 0) ? wordwrap($text, $maxword, ' ', true) : $text;

Example usage

// Here Add the cleanLongWords() function

$text = '\\\\\\\\\  A ttttttteeeeeeeeexxxxxxt with loooooonnnnnngggggg woooooooooooords and coooooonnnnnnsssssseeeeeeccccccuuuuuuttttttiiiiiivvvvvveeeeeee characteeeeeerrrrrrssssss [[[[[]]]]]] ///////......';

$text1 = cleanLongWords($text);
$text2 = cleanLongWords($text, 3);    // allow 3 consecutive repetitions of characters
$text3 = cleanLongWords($text, 1, 0);    // not consecutive characters, and not split the word

echo '$text1 - '. $text1;
echo '<br>$text2 - '. $text2;
echo '<br>$text3 - '. $text3;
- Result:
$text1 - \\ A tteexxt with loonngg woords and coonnsseeccuuttiivvee characteerrss [[]] //..
$text2 - \\\\\ A ttteeexxxt with looonnnggg wooords and cooonnnssseeecccuuuttti iivvveee characteeerrrsss [[[[[]]] ///...
$text3 - \ A text with long words and consecutive characters [] /.

Daily Test with Code Example

Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Delete multiple consecutive characters and Split long words

Last accessed pages

  1. Ajax-PHP Rating Stars Script (15976)
  2. sPBM - Simple PHP Backup Manager (2449)
  3. Update and Delete in MySQL Table (1129)
  4. Convert XML to JSON in PHP (10783)
  5. How to use php variable in external js file (1988)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (474)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (455)
  3. SHA256 Encrypt hash in JavaScript (390)
  4. Read Excel file data in PHP - PhpExcelReader (347)
  5. Create simple Website with PHP (330)