Jonathan Ross
Jonathan Ross

Reputation: 187

Break up one line of text without any discernible break points in PHP

I have a PHP function to catch long URLs and trim them so they don't break my site's formatting. I have wordwrap catching sentences but I can't stop text like this:

'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

How would I stop user input like that ?

The things I've tried work if there's dashes present (and I've tried ­ too in some functions) but none affect the above. It's for a forum and this is the last bit of user input I can't stop breaking my site ! Thanks for your help in advance.

Upvotes: 2

Views: 170

Answers (4)

Brian
Brian

Reputation: 8616

Basically what you want to do is implement a Markov Chain or Bloom Filter to try and identify 'gibberish' as such.

Forget where I got this now, but I have used this small function:

echo contains_gibberish( "heiahihaiaheiah" );

function contains_gibberish( $input )
{
    $result = array();

    for($i = 0; $i < strlen( $input ); $i++)
    {
        if ( isset( $result[ $input[ $i ] ] ) )
        {
            $result[ $input[ $i ] ]++;
        } else {
            $result[ $input[ $i ] ] = 1;
        }
    }
    return ( max( $result ) / strlen( $input ) * 100 >= 33 ) ? true : false;
}

This will allow you to flag input as potentially garbage, alternatives include wrapping the text etc.

Upvotes: 1

Shef
Shef

Reputation: 45589

<?php
$string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

echo chunk_split($string, $char_length = 50, $line_ending = "<br />");
?>

Output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Upvotes: 1

George Cummins
George Cummins

Reputation: 28906

wordwrap() will break up long words if you include TRUE as the fourth parameter:

wordwrap( $very_long_string, 30, '<br />', TRUE );

This will insert a break at or before 30 characters. TRUE is required to force a break in long words. If FALSE (the default) is used, the lines are broken only on spaces.

Upvotes: 2

Spudley
Spudley

Reputation: 168685

Rather than trying to break the word in PHP, you can use CSS to style the page such that it breaks long words when they don't fit into the available space.

The CSS style for this is word-wrap:break-word;.

Apply this to the element that contains the long text, and it will wrap long words when necessary.

Hope that helps.

Upvotes: 3

Related Questions