


How can I turn plain URLs in a string into clickable hyperlinks with PHP?
Oct 26, 2024 pm 12:22 PMLinking URLs in Strings with PHP
Linking URLs in strings can be a useful task in PHP for tasks such as generating clickable links in textual content. One common use case is converting a plain string containing URLs into HTML with clickable hyperlinks.
Syntax:
1 2 3 4 5 |
|
Explanation:
- Regex Pattern: The pattern used in preg_replace matches strings starting with a protocol prefix (e.g., "http://") followed by one or more alphanumeric characters, excluding angle brackets and whitespace. It ends with alphanumeric characters or a slash.
- Replacement String: The replacement string includes HTML markup to create an anchor (<a>) tag with the matched URL as the href attribute and the original URL as the text content.
Example:
1 2 3 4 5 6 7 8 |
|
PHP Versions:
This solution is compatible with both PHP versions prior to 5.3 (using ereg_replace) and PHP 5.3 and later (using preg_replace).
The above is the detailed content of How can I turn plain URLs in a string into clickable hyperlinks with PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
