在 PHP 中分解一行文本,没有任何明显的断点
我有一个 PHP 函数来捕获长 URL 并修剪它们,这样它们就不会破坏我网站的格式。我有自动换行捕捉句子,但我无法停止这样的文本:
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈)
我该如何停止这样的用户输入?
如果存在破折号,我尝试过的方法会起作用(并且我在某些函数中也尝试过 ­
),但不会影响上述内容。这是一个论坛,这是用户输入的最后一点,我无法停止破坏我的网站!提前感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 CSS 来设置页面样式,以便在长单词不适合可用空间时将其打断,而不是尝试在 PHP 中打断单词。
其 CSS 样式为
word-wrap:break-word;
。将此应用于包含长文本的元素,必要时它将包裹长单词。
希望有帮助。
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.
wordwrap()
会分解长单词,如果你包含TRUE
作为第四个参数:这将在 30 个字符处或之前插入一个分隔符。需要
TRUE
来强制中断长单词。如果使用FALSE
(默认值),则仅在空格处换行。wordwrap()
will break up long words if you includeTRUE
as the fourth parameter:This will insert a break at or before 30 characters.
TRUE
is required to force a break in long words. IfFALSE
(the default) is used, the lines are broken only on spaces.输出:
啊啊啊啊aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa呜呜呜呜呜
呜呜呜
Output:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
基本上,您想要做的是实现马尔可夫链或布隆过滤器来尝试识别“乱码”。
忘记我现在从哪里得到这个,但我使用了这个小函数:
echo contains_gibberish( "heiahihaiaheiah" );
这将允许您将输入标记为潜在的垃圾,替代方案包括换行文本等。
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" );
This will allow you to flag input as potentially garbage, alternatives include wrapping the text etc.