Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CSS中包装长文本_Php_Javascript_Jquery_Css - Fatal编程技术网

Php 在CSS中包装长文本

Php 在CSS中包装长文本,php,javascript,jquery,css,Php,Javascript,Jquery,Css,我有像这样的文字 <div style="float:left; width: 250px"> PellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesque feugiat tempor

我有像这样的文字

<div style="float:left; width: 250px"> PellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesquePellentesque  feugiat tempor elit. 
Ut mollis lacinia quam. Sed pharetra, augue aliquam   ornare vestibulum, metus massalaoreet tellus, eget iaculis lacus ipsum et diam. </div>
pellentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquentsquet封建主义临时精英。
莫利斯·拉西尼亚·奎姆。塞德·法雷特拉、奥古斯·阿利夸姆·奥纳雷前庭、梅特斯·马萨劳里特·泰勒斯、埃吉特·艾库利斯·拉库斯·伊普斯姆等。
我不想水平滚动。是否可以包装文本(自动换行)。我知道有一些IE特有的特性

谢谢你抽出时间


更新:我也可以使用jQuery、Javascript、PHP来完成这项工作。但是怎么做呢?我的意思是字母(字体)不是固定宽度或你所说的任何东西。

不幸的是,除非你对只支持特定浏览器(IE7/Win、Safari、Firefox 3.5)感到满意,否则你的问题没有纯CSS解决方案

换行:断开单词有效,但仅适用于IE

如果您能够更改文本,无论是服务器端(PHP、ASP)还是客户端(Javascript),您都可以编写一个小函数,在文本中插入“shy连字符”(­;)。这样,文本可以在每个连字符实例中被拆分,如果单词没有被拆分,则不会显示害羞连字符

编辑,例如:

bla&shy;bla&shy;bla&shy;bla&shy;bla&shy;bla&shy;bla&shy;bla&shy;bla&shy; (etc)
将在浏览器中显示如下内容:

blablabla-
blablabla-
blabla

您必须求助于JavaScript并使用如下函数:

<script language="javascript">
function wrap() {
    var data = document.getElementsByTagName('yourtaghere'); 
    var  desiredLength = 40 ;
    var delimiter = "<br />";
    for( var i=0; i < data.length; ++i ) {
        cellLength=data[i].innerHTML.length
        if( desiredLength < cellLength ) {
            var counter=0;
            var output="";
            while( counter < cellLength ) {
                output += data[i].innerHTML.substr(counter,desiredLength) + delimiter;
                counter+= desiredLength;
            }
            data[i].innerHTML=output;
        }
    }
}
window.onload=wrap;
</script>

函数换行(){
var data=document.getElementsByTagName('yourtaghere');
var desiredLength=40;
var分隔符=“
”; 对于(变量i=0;i
或者你可以用

我用这个组合

word-wrap: break-word;
overflow: hidden;

来处理这个问题。
word wrap
设置将允许在支持该属性的浏览器中对单词进行包装,而
overflow
设置将使其在无法识别
word wrap
的浏览器中的可用空间末尾被切断。这与不使用javascript的情况下可能出现的降级一样优雅。

一个新的答案,因为您已经更改了问题:

这是一个非常简单的PHP解决方案:

<?php
$string = "AnExtremelyLongStringWithoutWhitespacesOrBreakpointsOfAnyKindThatWillCompletelyAndUtterlyRuinYourWebsiteDesign";
for($i=0; $i<strlen($string); $i++) {
  $newString .= $string[$i] . '&shy;';
}
echo $newString;

作为Duroth答案的扩展,您可以使用以下php代码插入害羞连字符
$longest_length = 15;

$string_with_long_word = 'short lonngerrrrr lonnnnnnnggeeeeeeeeeeeeeeeeeessssssssssssssssstttttttttttttt and another looooooooooooonnnnnnnnngwwooooooooororoooorrrrrd';
$string = preg_replace_callback("/[a-z0-9]{{$longest_length},}/", 'putShyHyphen', $string_with_long_word);

function putShyHyphen($matches) {
 $string = $matches[0];
 $newstring = '';
 for ($i=0; $i<strlen($string); $i++) {
  $newstring .= $string[$i] . '&shy;';
 }

 return $newstring;
}
echo $string_with_long_word . '<hr />';
echo $string;
$longest_length=15;
$string_随以_long_word='short lonngerrr lonnnnnnggeeeeeeeessssssstttttttttttt和另一个loooooooooooooooonnnnnnnngwooooooooooroorooorrrd';
$string=preg_replace_回调(“/[a-z0-9]{{{$longest_length},}/”,'putshyphen',$string_与_long_单词);
函数putshyphen($matches){
$string=$matches[0];
$newstring='';
对于($i=0;$i简单css。请尝试以下操作:
将文本放入
..

css规则

pre {
   overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
   white-space: pre-wrap; /* css-3 */
   white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
   white-space: -pre-wrap; /* Opera 4-6 */
   white-space: -o-pre-wrap; /* Opera 7 */
   /* width: 99%; */
   word-wrap: break-word; /* Internet Explorer 5.5+ */
}

建议的标记可以打断很长的单词(或URL),但在浏览器之间尝试以窄div格式传输文本时,会得到混合结果。例如,200px div:

1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890

在Chrome中,数字中断了,在Firefox中,你没有得到两列数字。看起来Firefox没有考虑在“长”字的前一行上有多少可用的空间——它不会在任何机会中断。如果这种行为被纠正,我希望Firefox也会考虑这会如何影响文本的正当性。

“shy”标签有助于Firefox,但你仍然会收到“粗糙”的文本。我还没有做过广泛的测试,但当在Firefox中使用短词(少于8个字符)时,shy标签似乎会产生不可预测的结果(div失去其风格)。我个人更喜欢Chrome更具攻击性的中断!

根据

wordbreak:break all;

…与word wrap(现在更名为
overflow wrap
)的功能不同,因为即使是小字在行尾也可能会断开。但它可以工作


在一些罕见的情况下,
hyphen:auto;
可能会为您带来更好的结果,但前提是您的长单词包含在浏览器使用的词典中。

作为
换行:在宽度不固定的情况下(在
中更是如此),断开单词的效果不好,Stack Exchange添加了一些不可见的Unicode标记,浏览器使用这些标记查找换行符的可能位置。但是:当访问者复制文本时,这些不可见的标记仍然存在,这可能不好。有关Meta的详细信息,请参阅


在复制结果文本时,每隔几个字符插入
似乎不会产生坏的效果,而不是Unicode魔术。这在Chrome 25、Firefox 19、Safari 6、Internet Explorer 9(在Mac上并行运行的Windows 7中;模拟IE8和IE7也能起作用)中适用,Android 4.1上的股票浏览器和Chrome,以及iOS 6.1.2上的Safari,并且是。请参阅


总有一天,
将成为解决方案,但是:尽管IE7支持它,IE8和IE9却不支持。

真正的解决方案是“一开始就不要有太长的单词”。我无法控制用户不定时键入的单词。但是,您应该控制系统接受的单词。通常使用正则表达式来检测过长的单词(按字符数)并在服务器端插入空格,在页面中包含文本的位置将它们分隔开。但是,如果文本可能有标记(例如指向长URL的链接),则这可能会更棘手。类似的:这是真的,h