PHP converts the URL in the text to the auolink () function of the link.

Source: Internet
Author: User
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 about how PHP code converts a URL address to a URL address, see my article converting a URL address in text into a clickable JavaScript or php udf. link method, 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: 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:

The code is as follows:


<? Php

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

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

?>

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 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; // http://example.com/

?>

How about it, easy to use it!

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.