Break up one line of text without any discernible

2019-07-13 08:11发布

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.

4条回答
淡お忘
2楼-- · 2019-07-13 08:22

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-13 08:27
<?php
$string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

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

Output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

查看更多
时光不老,我们不散
4楼-- · 2019-07-13 08:28

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.

查看更多
Bombasti
5楼-- · 2019-07-13 08:39

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.

查看更多
登录 后发表回答