Hàm wordwrap[]
sẽ thêm một kí tự hoặc một thẻ nào đó vào chuỗi khi đã đếm đủ số kí tự nhất định. Ví dụ cứ sau mỗi 20 kí tự ta sẽ xuống dòng, trong trường hợp này ta sẽ sử dụng hàm wordwrap[]
để xử lý.
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Cú pháp
Cú pháp: wordwrap[ $str, $width, $break, $cut];
Trong đó:
$str
là chuỗi cần xử lý..$width
là độ dài mà cứ sau$width
kí tự sẽ thêm$break
vào chuỗi$str
.$cut
là tham số. Mặc định là FALSE.$cut = TRUE
, cứ$width
kí tự trong chuỗi thì sẽ thêm$break
vào vị trí tiếp theo.$cut = FALSE
, Hàm sẽ không cắt ngang các từ kể cả khi đã vượt quá$width
kí tự.
Cách sử dụng hàm wordwrap:
Bài viết này được đăng tại [free tuts .net]
Code
$text = "this is a test stringgggggggggggggggggggggggg"; echo $newtext = wordwrap[$text, 20, "-"]."
"; echo $newtext = wordwrap[$text, 8, "-", true]."
"; echo $newtext = wordwrap[$text, 8, "-", false]."
";
Kết quả
this is a test-stringgggggggggggggggggggggggg this is-a test-stringgg-gggggggg-gggggggg-gggggg this is-a test-stringgggggggggggggggggggggggg
Tham khảo: php.net
- Trang chủ
- Tham khảo
- CSS
- CSS3
- Thuộc tính word-wrap
Định nghĩa và sử dụng
Thuộc tính word-wrap sẽ làm cho những từ dài xuống hàng mà không làm vỡ layout.
Cấu trúc
tag { word-wrap: giá trị; }
word-wrap có các giá trị như sau:
word-wrap | break-word | word-wrap: break-word; | Những từ quá dài sẽ xuống hàng. |
normal | word-wrap: normal; | Trả về dạng mặc định ban đầu cho word-wrap. |
Ví dụ
HTML viết:
HỌC WEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEB CHUẨN
CSS viết:
p { border: 1px solid #cc0000; width: 150px; }
Hiển thị trình duyệt khi chưa có word-wrap:
HỌC WEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEB CHUẨN
Thêm thuộc tính word-wrap vào CSS:
p { border: 1px solid #cc0000; width: 150px; word-wrap: break-word; }
Hiển thị trình duyệt khi có word-wrap:
HỌC WEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEB CHUẨN
Hàm wordwrap[]
sẽ thêm một kí tự hoặc một thẻ nào đó vào chuỗi khi đã đếm đủ số kí tự nhất định. Ví dụ cứ sau mỗi 20 kí tự ta sẽ xuống dòng,
trong trường hợp này ta sẽ sử dụng hàm wordwrap[]
để xử lý.
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Cú pháp
Cú pháp: wordwrap[ $str, $width, $break, $cut];
Trong đó:
$str
là chuỗi cần xử lý..$width
là độ dài mà cứ sau$width
kí tự sẽ thêm$break
vào chuỗi$str
.$cut
là tham số. Mặc định là FALSE.$cut = TRUE
, cứ$width
kí tự trong chuỗi thì sẽ thêm$break
vào vị trí tiếp theo.$cut = FALSE
, Hàm sẽ không cắt ngang các từ kể cả khi đã vượt quá$width
kí tự.
Cách sử dụng hàm wordwrap:
Bài viết này được đăng tại [free tuts .net]
Code
$text = "this is a test stringgggggggggggggggggggggggg"; echo $newtext = wordwrap[$text, 20, "-"]."
"; echo $newtext = wordwrap[$text, 8, "-", true]."
"; echo $newtext = wordwrap[$text, 8, "-", false]."
";
Kết quả
this is a test-stringgggggggggggggggggggggggg this is-a test-stringgg-gggggggg-gggggggg-gggggg this is-a test-stringgggggggggggggggggggggggg
Tham khảo: php.net
[PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8]
wordwrap — 打断字符串为指定数量的字串
说明
wordwrap[
string
$str
,
int $width
= 75,
string $break
= "\n",
bool $cut
= false
]: string
参数
str
输入字符串。
width
列宽度。
break
使用可选的 break
参数打断字符串。
cut
如果 cut
设置为 true
,字符串总是在指定的 width
或者之前位置被打断。因此,如果有的单词宽度超过了给定的宽度,它将被分隔开来。(参见第二个范例)。 当它是 false
,函数不会分割单词,哪怕 width
小于单词宽度。
范例
示例 #1 wordwrap[] 范例
以上例程会输出:
The quick brown fox
jumped over the lazy
dog.
示例 #2 wordwrap[] 范例
以上例程会输出:
A very long wooooooo ooooord.
示例 #3 wordwrap[] 例子
以上例程会输出:
A very long woooooooooooooooooord. and something
参见
- nl2br[] - 在字符串所有新行之前插入 HTML 换行标记
- chunk_split[] - 将字符串分割成小块
Alhadis ¶
7 years ago
For those interested in wrapping text to fit a width in *pixels* [instead of characters], you might find the following function useful; particularly for line-wrapping text over dynamically-generated images.
If a word is too long to squeeze into the available space, it'll hyphenate it as needed so it fits the container. This operates recursively, so ridiculously long words or names [e.g., URLs or this guy's signature - //en.wikipedia.org/wiki/Wolfe+585,_Senior] will still keep getting broken off after they've passed the fourth or fifth lines, or whatever.
ju1ius ¶
10 years ago
Another solution to utf-8 safe wordwrap, unsing regular expressions.
Pretty good performance and works in linear time.
Of course don't forget to use preg_quote on the $width and $break parameters if they come from untrusted input.
Dave Lozier - dave at fusionbb.com ¶
16 years ago
If you'd like to break long strings of text but avoid breaking html you may find this useful. It seems to be working for me, hope it works for you. Enjoy. :]
frans-jan at van-steenbeek dot R-E-M-O-V-E dot net ¶
16 years ago
Using wordwrap is usefull for formatting email-messages, but it has a disadvantage: line-breaks are often treated as whitespaces, resulting in odd behaviour including lines wrapped after just one word.
To work around it I use this:
I then use linewrap[] instead of wordwrap[]
hope this helps someone
michdingpayc ¶
2 months ago
A correction to ju1ius' utf-8 safe wordwrap from 10 years ago.
This version addresses issues where breaks were not being added to the first and last words in the input string.
altin_bardhi at yahoo dot co dot uk ¶
11 years ago
Here I have come out with a possibly very useful wordwrap code snippet.
Apparently what this piece of code does is: it takes the entered text and looks for words longer than the defined ‘$chunk_length’ if it finds any, it splits the long words and then it concatenates the whole string back to a new string with longer words separated by a dash character in this case.
After it has accomplished this task it then inserts an HTML line break after a specified ‘$line_length’ [Depending on your containers width requirements]
Peter ¶
15 years ago
The main concern when you have a text in a cell is for long words that drags the cell margins. This function will break words in a text that have more then $nr characters using the "-" char.
answers at clearcrescendo.com ¶
3 years ago
wordwrap[] uses the break string as the line break detected, and the break inserted, so your text must be standardized to the line break you want in the wordwrap[] output before using wordwrap, otherwise you will get line breaks inserted regardless of the location of existing line breaks in your text.
$ php -f test.php
Standardized EOL:
abc abc abc
abc abc abc
abc abc abc
Wrapped at 5:
abc abc
abc
abc abc
abc
abc abc
abc
$del=' at '; 'sanneschaap' dot $del dot 'gmail dot com' ¶
14 years ago
These functions let you wrap strings comparing to their actual displaying width of proportional font. In this case Arial, 11px. Very handy in some cases since CSS3 is not yet completely supported. 100 strings = ~5 ms
My old sheep word wrap function [posted at the bottom of this page, is kinda old dated and this one is faster and more accurate].