InThe following is an example of converting URL link addresses in text to HTML code. Code 6.4 is an example of PHP function Preg_match_all ().
Code 6.4 Convert the link address in the text to HTML
- < ? PHP
- Function: Convert the link address in text to HTML
- Input: String
- Output: String
- function url2html ($text)
- {
- Match a URL until a blank is present
- Preg_match_all ("/http://?[ ^s]+/i "
, $text, $links);
- Set page display URL address length
- $ max_size = + ;
- foreach ($links [0] as $link _url)
- {
- Calculates the length of the URL. If the setting exceeds $max_size, it is shortened.
- $ Len = strlen ($link _url);
- if ($len > $max _size)
- {
- $ Link_text = substr ($link _url, 0, $max _size). " ...";
- } else {
- $ Link_text = $link _url;
- }
- Generate HTML text
- $ text = Str_replace ($link _url, "
< a href=' $link _url '>$link _text a>", $text);
- }
- return $text;
- }
- Running an instance
- $ Str = "This is a multiline text with multiple URL link addresses."
Welcome to visit http://www.taoboor.com ";
- Print url2html ($STR);
- /* Output Results
- This is a multiline text with multiple URL link addresses
。 Welcome to visit < a href=' http://www.taoboor.com ' >
- http://www.taoboor.com a >
- */
- ?>
I hope you can fully grasp the application of this function by using the example of PHP function Preg_match_all () above.
http://www.bkjia.com/PHPjc/446251.html www.bkjia.com true http://www.bkjia.com/PHPjc/446251.html techarticle The following is an example of converting URL link addresses in text to HTML code. Code 6.4 is an example of PHP function Preg_match_all (). Code 6.4 Converts the link address in the text to H ...