The PHP implementation converts the URL in the text to a linked Auolink () function to share the _php instance

Source: Internet
Author: User

In fact, I in the text of the URL address into clickable link JavaScript, PHP custom function in the article describes how the PHP code to achieve the URL address into a link to the method, today to introduce a more concise version, first look at the source code of PHP:

Auolink () API

Copy Code code 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<a href= ' $ ' rel= ' external nofollow '. $attrs. ' >$2</a> ', $str);
$str = substr ($str, 1);

return $str;
}

How, very concise! Look at the API documentation for the function:

Grammar

String Autolink (String $str [, Array $attributes = Array ()])

Parameter introduction

str– Required (String type data). You need to query for alternate text.
Attributes-Optional (Array type data). Replaces some of the optional parameters of a link.

return value

Returns the replaced text.

Autolink () Call method

Autolink is also very convenient to use, we can only pass one parameter, that is, required to replace the character text. For example:

Copy Code code as follows:

<?php

$str = ' A link:http://example.com/?param=value#anchor. ';
$str = Autolink ($STR);

Echo $str; A Link: <a href= "Http://example.com/?param=value#anchor" rel= "External nofollow" >http://example.com/?param= Value#anchor</a>.

?>

In addition, we can set up some additional link parameters to allow the generated link to open in a new window or do not want the search engine index to replace the link. For example:

Copy Code code as follows:

<?php

$str = ' http://example.com/';
$str = Autolink ($str, Array ("target" => "_blank", "rel" => "nofollow"));

Echo $str; <a href= "http://example.com/" rel= "external nofollow" target= "_blank" >http://example.com/</a>

?>

How, convenient and 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.