php function Preg_match_all () about how to use the _php tutorial

Source: Internet
Author: User
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

 
 
  1. < ? PHP
  2. Function: Convert the link address in text to HTML
  3. Input: String
  4. Output: String
  5. function url2html ($text)
  6. {
  7. Match a URL until a blank is present
  8. Preg_match_all ("/http://?[ ^s]+/i "
    , $text, $links);
  9. Set page display URL address length
  10. $ max_size = + ;
  11. foreach ($links [0] as $link _url)
  12. {
  13. Calculates the length of the URL. If the setting exceeds $max_size, it is shortened.
  14. $ Len = strlen ($link _url);
  15. if ($len > $max _size)
  16. {
  17. $ Link_text = substr ($link _url, 0, $max _size). " ...";
  18. } else {
  19. $ Link_text = $link _url;
  20. }
  21. Generate HTML text
  22. $ text = Str_replace ($link _url, "
    < a href=' $link _url '>$link _text a>", $text);
  23. }
  24. return $text;
  25. }
  26. Running an instance
  27. $ Str = "This is a multiline text with multiple URL link addresses."
    Welcome to visit http://www.taoboor.com ";
  28. Print url2html ($STR);
  29. /* Output Results
  30. This is a multiline text with multiple URL link addresses
    。 Welcome to visit < a href=' http://www.taoboor.com ' >
  31. http://www.taoboor.com a >
  32. */
  33. ?>

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.