Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $link = 'Link #1 = http://mynewsitepreview.com/ and Link #2 = www.mynewsitepreview.com';
- echo findlinks($link);
- function findlinks($text) {
- $email_pattern = "/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i";
- $url_pattern = "/((http|https|ftp|sftp):\/\/)[a-z0-9\-\._]+\/?[a-z0-9_\.\-\?\+\/~=&#;,]*[a-z0-9\/]{1}/si";
- $www_pattern = "/[^>](www)[a-z0-9\-\._]+\/?[a-z0-9_\.\-\?\+\/~=&#;,]*[a-z0-9\/]{1}/si";
- // First, check if the string contains an email address...
- if( preg_match( $email_pattern, $text, $email ) ) {
- $replacement = '<a href="mailto:' . $email[0]. '">' . $email[0] . '</a> ';
- $text = preg_replace($email_pattern, $replacement, $text);
- }
- // Next, check if the string contains a URL beginning with http://, https://, ftp://, or sftp://
- if( preg_match( $url_pattern, $text, $url ) ) {
- $replacement = '<a href="' . $url[0] . '">' . $url[0] . '</a>';
- $text = preg_replace($url_pattern, $replacement, $text);
- }
- // Last, check for a plain old www address (without a closing HTML tag before them)
- if( preg_match( $www_pattern, $text, $www ) ) {
- $replacement = ' <a href="http://' . ltrim( $www[0] ). '">' . ltrim( $www[0] ) . '</a> ';
- $text = preg_replace($www_pattern, $replacement, $text);
- }
- return $text;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement