How to linking in php

This article will explore different techniques to automatically generate HTML links with PHP.

Conversions from URL to link are commonly used with user-generated content. For example, you can apply this technique to blog comments or forum areas.

We can perform this operation in two steps. First, we have to detect the URLs in the text; then, we must replace the URLs with link elements that use the considered URL both in the src attribute and in the anchor text.

The best solution to perform this kind of replacement is to use the PCRE functions provided by PHP. In particular, this example uses the preg_replace() PHP function, which allows us to find the match and apply the replacement with a single instruction.

In the example below, the convert() function replaces all the URLs available in the provided string with links.

/** * This functions converts the URLs available in the provided string to links. * * @param $s * * @return array|string|string[]|null */ function convert($s) < //A regular expression to find the URLs $url_regex = '\.[a-zA-Z0-9()]\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)>'; //The preg_replace() function applies the links by using the captured groups of the URL regex return preg_replace($url_regex, '$0', $s); > $html = 'My website is http://example.com'; echo convert($html); //Output: My website is http://example.com

The PHP Autolink library is available on GitHub, and you can install it in a few seconds in your PHP environment with Composer.

Читайте также:  Html script on page load

With this library, you can convert URLs or email addresses to links with the two provided methods, convert() and convertEmail() .

The example below uses this library to convert all the URLs included in the $html string to links.

Automatically convert email addresses to links

It’s sometimes convenient to convert the email addresses included on a page to links that open the email client. Let’s explore two implementation methods.

In this custom implementation, I use a regular expression provided by this specialized website to find all the emails in the string. Note that this time the value of the href attribute is composed by the prefix mailto: followed by the email address.

/** * This function converts the email addresses available in the provided string to URLs. * * @param $s * * @return array */ function email_to_link($s) < //A regular expression that matches the email addresses $email_regex = '/([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z])/i'; //A link with the "mailto:" prefix return preg_replace($email_regex, '$0', $s); > $html = 'My email address is example@gmail.com'; echo email_to_link($html); //Output: My email address is example@gmail.com

This example uses the PHP Autolinks library again, but this time with convertEmail() . This method converts all the emails in the string to links.

This section aims to illustrate methods to automatically replace specific keywords to link.

In the real world, automatic links generated on a list of defined keywords are commonly used to increase the number of internal links, create affiliate links, or link specific glossary terms to their definitions.

Replace the keywords with a custom solution

We can complete this process in three steps:

  1. Create a list of keywords with the related URLs
  2. Iterate the list of keywords
  3. Replace the keywords with links

Create an array that includes the keywords and the related URLs.

//The keywords with the related URLs $data_a = [ [ 'keyword' => 'book', 'url' => 'https://example.com/book/' ], [ 'keyword' => 'amazon', 'url' => 'https://example.com/amazon/' ] ];

Then write a function that converts the single keywords to links:

Now you can iterate over the keyword data to automatically apply the links.

$html = 'My book is published on Amazon.'; //Iterate over the keyword data foreach($data_a as $data) < //Convert this specific keyword to a link $html = convert($html, $data); >//Echo the resulting HTML echo $html;

In the resulting output, the keywords in the string have been replaced with links:

Automatic Links is a PHP class from our portfolio that we initially developed to implement the automatic links functionalities of the Interlinks Manager WordPress plugin.

The main advantage of using this library is that you have many customization options to control the replacements of the keywords. For example, you can limit the number of automatic links, protect specific HTML tags, prevent multiple links to the same destination, etc.

The example below uses this class to replace all the occurrences of the keyword “iPhone” with links.

Источник

symlink() creates a symbolic link to the existing target with the specified name link .

Parameters

Return Values

Returns true on success or false on failure.

Errors/Exceptions

The function fails, and issues E_WARNING , if link already exists. On Windows, the function also fails, and issues E_WARNING , if target does not exist.

Examples

Example #1 Create a symbolic link

$target = ‘uploads.php’ ;
$link = ‘uploads’ ;
symlink ( $target , $link );

See Also

  • link() — Create a hard link
  • readlink() — Returns the target of a symbolic link
  • linkinfo() — Gets information about a link
  • unlink() — Deletes a file

User Contributed Notes 20 notes

Here is a simple way to control who downloads your files.

You will have to set: $filename, $downloaddir, $safedir and $downloadURL.

Basically $filename is the name of a file, $downloaddir is any dir on your server, $safedir is a dir that is not accessible by a browser that contains a file named $filename and $downloadURL is the URL equivalent of your $downloaddir.

The way this works is when a user wants to download a file, a randomly named dir is created in the $downloaddir, and a symbolic link is created to the file being requested. The browser is then redirected to the new link and the download begins.

The code also deletes any past symbolic links created by any past users before creating one for itself. This in effect leaves only one symbolic link at a time and prevents past users from downloading the file again without going through this script. There appears to be no problem if a symbolic link is deleted while another person is downloading from that link.

This is not too great if not many people download the file since the symbolic link will not be deleted until another person downloads the same file.

$letters = ‘abcdefghijklmnopqrstuvwxyz’ ;
srand ((double) microtime () * 1000000 );
$string = » ;
for ( $i = 1 ; $i $q = rand ( 1 , 24 );
$string = $string . $letters [ $q ];
>
$handle = opendir ( $downloaddir );
while ( $dir = readdir ( $handle )) <
if ( is_dir ( $downloaddir . $dir )) <
if ( $dir != «.» && $dir != «..» ) <
@ unlink ( $downloaddir . $dir . «/» . $filename );
@ rmdir ( $downloaddir . $dir );
>
>
>
closedir ( $handle );
mkdir ( $downloaddir . $string , 0777 );
symlink ( $safedir . $filename , $downloaddir . $string . «/» . $filename );
Header ( «Location: » . $downloadURL . $string . «/» . $filename );
?>

Источник

PHP code

Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms.

Websites are filled with links. You’re probably already aware of how to create a link in HTML. If you’ve added PHP to your web server to be able to enhance your site’s capabilities, you may be surprised to learn that you create a link in PHP the same as you do in HTML. You have a few options, though. Depending on where in your file the link is, you might present the link HTML in a slightly different way.

You can switch back and forth between PHP and HTML in the same document, and you can use the same software—any plain text editor will do—to write PHP as to write HTML.

If you are making a link in a PHP document that is outside of the PHP brackets, you just use HTML as usual. Here is an example:

If the link needs to be inside the PHP, you have two options. One option is to end the PHP, enter the link in HTML, and then reopen PHP. Here is an example:

The other option is to print or echo the HTML code inside the PHP. Here is an example:

Another thing you can do is create a link from a variable. Let’s say that the variable $url holds the URL for a website that someone has submitted or that you have pulled from a database. You can use the variable in your HTML.

For Beginning PHP Programmers

If you are new to PHP, remember you begin and end a section of PHP code using and ?> respectively. This code lets the server know that what is included is PHP code. Try a PHP beginner’s tutorial to get your feet wet in the programming language. Before long, you’ll be using PHP to set up a member login, redirect a visitor to another page, add a survey to your website, create a calendar, and add other interactive features to your webpages.

Источник

Use the link() function in PHP to create a hard link.

Syntax

Parameters

  • target − The target of the link. Required.
  • link − The name of the link. Required.

Return

The link() function returns TRUE on success or FALSE on failure.

Example

Output

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • How to effectively hide Href From a Link in PHP?
  • filter_has_var() function in PHP
  • filter_id() function in PHP
  • filter_input() function in PHP
  • filter_input_array() function in PHP
  • filter_list() function in PHP
  • filter_var_array() function in PHP
  • filter_var() function in PHP
  • constant() function in PHP
  • define() function in PHP
  • defined() function in PHP
  • die() function in PHP
  • eval() function in PHP
  • exit() function in PHP
  • get_browser() function in PHP

Annual Membership

Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses

Training for a Team

Affordable solution to train a team and make them project ready.

Tutorials PointTutorials Point

  • About us
  • Refund Policy
  • Terms of use
  • Privacy Policy
  • FAQ’s
  • Contact

Copyright © Tutorials Point (India) Private Limited. All Rights Reserved.

We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more

Источник

Оцените статью