Battling ‘Line Spam’ Using PHP

Tuesday, February 10th, 2009

How frustrated do you get when annoying people post "ffffffffffffffffff…ffff" on your blog/forum? And this piece of line annoyingly stretches the page.

How many times have you researched for a solution but realize that some work well on firefox but messes up on internet explorer whereas some work smoothly on safari but messes up on opera?

I've ran so many tests here and there on existing websites. Sadly but true, even the forms created by wordpress allow people to post such content.

I've decided to share this because:

  1. it frustrated the living hell out of me :)
  2. I don't want you to waste time while developing.

Some decide to use the PHP function wordwrap() function to just wrap a string to a given number of characters using a string break character. BUT this doesn't do the trick!

Some decide to use the PHP chunk_split() function to split a string into smaller chunks. BUT this doesn't do the trick!

After intense research, I came up with a solution which works perfectly on all browsers and doesn't even stretch the page.

I was looking for something which will break up words of that sort (i.e: ffffffff, aaaaaaaa, ect. ) but at the same time wrap it at a certain fixed length.

Here's a function that adds a space (" ") after every $i characters, if no space is found until that character, the browser will automatically wrap the text:

<?php

function layout_wrap($str, $i) {
$j = $i;
while ($i < strlen($str)) {
if (strpos($str, ' ', $i-$j+1) > $i+$j || strpos($str, ' ', $i-$j+1) === false) {
$str = substr($str, 0, $i) . ' ' . substr($str, $i);
}
$i += $j;
}
return $str;
}

?>

But if you try the above, it will not work since it's still not enough. It's true that words are cut after every $i character but it still stretches the page. Another interesting function is the following:

function utf8_wordwrap($str,$width,$break, $cut){
return utf8_encode(wordwrap(utf8_decode($str), $width, $break, $cut));
}

This will wrap the content at a certain given width but, same as the previous comment, it's not enough. Words are not being cut.

After many trials and errors, I've tried combining them since I was interested to have both features at the same time. This is what I came up with which works like a charm:

utf8_wordwrap(layout_wrap(str, 30), 75, "\n", false);

What will this do? Well, it will combine the best of 2 worlds.

  1. It will not exceed a width of 75
  2. Words of more than 30 characters will simply be cut.

Happy coding!


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

working . tweeting . reading . blogging . geeking . gaming . sporting . Founder of simplyconfess.com . Personal blog @ http://cli.gs/LpZvbt . twitter: @itsmb , @simplyconfess
  1. February 10, 2009 at 3:50 pm
  2. February 10, 2009 at 10:20 pm
  3. February 13, 2009 at 9:12 am
  4. February 14, 2009 at 9:36 am
  5. February 14, 2009 at 11:28 pm
  6. April 17, 2009 at 2:13 am
  7. Rob
    May 11, 2009 at 11:04 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates