Home > Backend Development > PHP Tutorial > 14. CSS - Automatically wrap a long string when it exceeds the width

14. CSS - Automatically wrap a long string when it exceeds the width

WBOY
Release: 2016-07-29 08:56:22
Original
1716 people have browsed it

1. Related information

When outputting a long string in PHP (the string has no line breaks), it is hoped that the string can automatically wrap when the content exceeds the set length, otherwise the string without line breaks will Exceeding the set width, the page will be stretched infinitely

In pure HTML code, even without any special declaration, it will automatically wrap according to the set width, for example

<code><span><<span>div</span><span>style</span>=<span>"width: 100px;"</span>></span>
测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试
<span></<span>div</span>></span></code>
Copy after login

The above code will when the content width exceeds 100px Will automatically wrap the output

But when outputting a long string of php

<code><span><<span>div</span><span>style</span>=<span>"width: 100px;"</span>></span><span><span><?php</span><span>echo</span><span>$long_content</span>; <span>?></span></span><span></<span>div</span>></span></code>
Copy after login

The content width of $long_content is far more than 100px, you can see that the content output will not automatically wrap the line, it will be based on the string The width of the page is infinitely stretched, that is, it will be stretched as long as it is. The defined width is imaginary, which is obviously not what we expect. 2. Problem description

When a long string is output, the content will not Automatic line wrapping will stretch the page infinitely

3. Solution

Use the following css definition

<code><span><<span>div</span><span>style</span>=<span>"width: 100px; word-break: break-all;word-wrap: break-word;"</span>></span><span><span><?php</span><span>echo</span><span>$long_content</span>; <span>?></span></span><span></<span>div</span>></span></code>
Copy after login

    word-break
  1. The attribute specifies the processing method of automatic line wrapping. The value break-all indicates that line breaks are allowed within words. . The
  2. word-wrap
  3. attribute allows long words or URL addresses to wrap to the next line. The value break-word means wrapping inside long words or URL addresses.
  4. ').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); The above has introduced 14. CSS - Automatically wrap a long string when it exceeds the width, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template