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:
- it frustrated the living hell out of me
- 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.
- It will not exceed a width of 75
- 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!









Looks great! How do you handle long URLs with this method?
Same concept. You will have to set a limit. Thus, the long URLs will also be chopped. But there’s a way around URLs since very long URLs are not advisable when it comes to viewing purposes.
There are many websites that shortens long URLs which you will realize is the common trend.
Some of these sites are:
- http://www.2lng.com/
- http://is.gd/
- http://tinyurl.com/
- http://cli.gs/ [ this also provides analytics, social media monitoring, and geotargeting ]
- http://zi.ma/
- many more.
There’s also a client-side JavaScript solution:
http://www.midorijs.com/midori.html#shortenWords
Hey, great tip, this will come in handy.
Thanks.
@Aycan Gulez : true, but the problem is that there are a lot of annoying people out there. they can simply disable javascript and write the frustrating comments that have no meaning at all just to ruin the design.
@Meshach: glad you like it!
G8 post! This is really helpful when I go with word wrapping.
I could be wrong, but…
Using the above method, you will lose any characters that are in UTF-8 but not in the ISO-8859-1 character set. An example being the euro currency character.
http://uk3.php.net/utf8_decode
This could be fixed by using iconv() to convert between character sets iconv() and / or using the multi-byte safe string functions