PHP: Add target="_blank" to (outgoing) HTML Links
Tip by Stefan Trost | Last update on 2021-07-01 | Created on 2013-02-03
In this tutorial, I show you a PHP solution to add the attribute target="_blank" to all (outgoing) links automatically.
This code can be used, for example, to edit a text that a user has entered into a comment form automatically and to ensure that all (outgoing) links will open in a new window or tab and your own page remains open in the browser even when a user clicks on one of these links.
Add attribut target="_blank" to all links
We make use of the PHP function preg_replace, which we feed with the following regular expression:
$l = '<a href="https://www.sttmedia.com">'; $l = preg_replace('/(<a href="[^"]+")>/is', '\\1 target="_blank">', $l); echo $l; // <a href="https://www.sttmedia.com" target="_blank">
So, we are searching for a link construct beginning with '<a href="' and ending with '">' and we are replacing this construct with the matching string including 'target="_blank"'.
Between the two double quotation marks after 'href=' an arbitrary text - in this case our link - can occur. The text found in the brackets - that is everything from '<a href...' to the closing quotation mark " - but without the closing > are fetched up again with \\1 in the replacement and inserted together with ' target="_blank">' instead of the hit.
Add attribute target="_blank" only to outgoing links
Assuming all outgoing links are starting with "http" or "https" while internal links are not containing the full domain name and are looking something like "/internalpage.html", "/internalpage" or "/internal/page.php" instead, we can expand our code in the following way:
$l = preg_replace('/(<a href="http:[^"]+")>/is', '\\1 target="_blank">', $l); $l = preg_replace('/(<a href="https:[^"]+")>/is', '\\1 target="_blank">', $l);
With this, the attribute will only be added to links referring to another website than the own one, other internal links remains untouched. As long as the visitors are browsing within the own domain, it is always used the current browser windows. As soon as they are going to leave the own domain, a new browser window is opened. Of course, this only works in this way if internal links are specified in the form mentioned without "http" or "https".
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
MySQL: Line Breaks in MySQL
Tip | 0 Comments
How to Replace multiple Texts at the same Time
Tutorial | 0 Comments
Windows Batch Script: Computer Shutdown
Tutorial | 2 Comments
HTML: Preassign HTML Form with Data
Tutorial | 0 Comments
jQuery: CSS Stylesheet Switcher
Tutorial | 1 Comment
CSS: Include CSS Stylesheets in HTML
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.