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