PHP converts the URL in the text to the auolink of the link.

Source: Internet
Author: User
: This article mainly introduces PHP to convert the URL in the text to the auolink of the link. if you are interested in the PHP Tutorial, refer to it. Reprinted: http://www.jb51.net/article/52916.htm

In fact, I have introduced how PHP code converts a URL address into a link in "converting a URL address in text into a clickable JavaScript or PHP UDF, today we will introduce you to a more concise version. let's take a look at the source code of PHP:

/**

* Author: SeeDZ

* From: http://code.seebz.net/p/autolink-php/

**/

Function autolink ($ str, $ attributes = array ()){

$ Attrs = '';

Foreach ($ attributes as $ attribute => $ value ){

$ Attrs. = "{$ attribute }=\" {$ value }\"";

}

$ Str = ''. $ str;

$ Str = preg_replace (''([^" = \ '>]) (http | https | ftp | ftps ): // [^ \ s <] + [^ \ s <\.)]) 'I', '$1 $ 2', $ str );

$ Str = substr ($ str, 1 );

Return $ str;

}

How about it? it's very concise! Take a look at the function API documentation:

Syntax

String autolink (string $ str [, array $ attributes = array ()])

Parameter introduction

Str-required (String type data ). The text to be replaced.
Attributes-optional (Array data ). Replace some optional parameters of the link.

Return value

Returns the replaced text.

Autolink () call method

Autolink is easy to use. we can pass only one parameter, that is, the required character text to be replaced. For example:

$ Str = 'a link: http://example.com /? Param = value # anchor .';

$ Str = autolink ($ str );

Echo $ str; // A link: http://example.com /? Param = value # anchor </a>.

?>

In addition, we can set some additional link parameters to enable the generated link to be opened in a new window, or do not want to searchIndexBytesIndexReplacement link. For example:

$ Str = 'http: // example.com /';

$ Str = autolink ($ str, array ("target" => "_ blank", "rel" => "nofollow "));

Echo $ str; // http://example.com/</a>

?>

The above describes how PHP converts the URL in the text to the auolink of the link, including some content, and hopes to help friends who are interested in the PHP Tutorial.

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.