This article mainly introduces how PHP converts the URL in the text to the auolink () function of the link. It is a very simple and easy-to-use function. The original author also has some Nice PHP functions, for more information, see
This article mainly introduces how PHP converts the URL in the text to the auolink () function of the link. It is a very simple and easy-to-use function. The original author also has some Nice PHP functions, for more information, see
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:
Auolink () API
The Code is as follows:
/**
* Author: SeeDZ
* From:
**/
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:
The Code is as follows:
<? Php
$ Str = 'A link :? Param = value # anchor .';
$ Str = autolink ($ str );
Echo $ str; // A link :? Param = value # anchor.
?>
In addition, we can set some additional link parameters to enable the generated link to open in a new window, or do not want to replace the search engine index. For example:
The Code is as follows:
<? Php
$ Str = 'HTTP: // example.com /';
$ Str = autolink ($ str, array ("target" => "_ blank", "rel" => "nofollow "));
Echo $ str ;//
?>
How about it, easy to use it!