简体   繁体   English

如何减少很长的字符串的长度?

[英]How to reduce the length of very long strings?

I have some text which occasionally contains very long strings of characters, which is breaking my CSS. 我有一些文本有时包含很长的字符串,这破坏了我的CSS。

This is an example: 这是一个例子:

http://iapps.smartphonesoft.com/php/software_details.php?id=358281487 http://iapps.smartphonesoft.com/php/software_details.php?id=358281487

whereas this one is fine 而这个很好

http://iapps.smartphonesoft.com/php/software_details.php?id=380894729 http://iapps.smartphonesoft.com/php/software_details.php?id=380894729

Is it possible to strip the very long line of *s in the above example down to a manageable length? 是否可以将上述示例中的* s的很长的行剥离为可管理的长度?

The extra complication is that is not always *s that cause the issue, ie in this example it is the = string which is causing the issue. 额外的麻烦是,并非总是* s导致问题,即在此示例中= =字符串导致了问题。

http://iapps.smartphonesoft.com/php/software_details.php?id=371255483 http://iapps.smartphonesoft.com/php/software_details.php?id=371255483

So I want to write PHP which carries out the function if a single 'word' within $description is >= 30 chars long, shrink it down to 30 chars. 因此,我想编写PHP,如果$ description中的单个“单词”的长度> = 30个字符,则执行该功能,并将其缩小为30个字符。

All the text is kept within the variable $description 所有文本都保存在变量$ description中

regards, 问候,

Greg 格雷格

By default, browsers won't wrap an extremely long word. 默认情况下,浏览器将不会包含一个很长的单词。 You can override this behavior and always wrap, even if you need to break a word, with CSS (no PHP required!) 您可以重写此行为,并始终使用CSS包装,即使您需要打断一个单词(也不需要PHP!)

The property is word-wrap: break-word; 该属性是word-wrap: break-word; and works in all browsers. 并适用于所有浏览器。

you can explode string to array of words, then check strlen of every word not > 30 您可以将字符串分解为单词数组,然后检查每个单词的strlen不大于30

like 喜欢

$words = explode(" ", $string);
foreach($words as $k=>$word){
   if(strlen($word) >= 30)
      $words[$k] = substr($word,0,30) ;
}

after that return array to one string with 之后,将数组返回到一个字符串

join(' ' ,$words);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM