Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

PHP/REGEX: Format Web, Email and Twitter Links

Using PHP and REGEX, I want to format web, email and Twitter links.

I have code that works with ASP.NET / VB but I need it to work with PHP.

<?php

//  Links
//  input = RegularExpressions.Regex.Replace(input, "(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")
//  input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")
//
//' Email Addresses
//  input = RegularExpressions.Regex.Replace(input, "\w[\w\.]*\w?@[\w\.]+\w", "<a href=""mailto:$0"">$0</a>")
//
//  Twitter
//  input = RegularExpressions.Regex.Replace(input, "(?<!\S)\((@)(\S+[^\s\)]+)\)", "(<a href=""http://twitter.com/$2"">$1$2</a>)")

$str = '
Hello test@example.com this is a test.  Visit www.example.com or http://example.com for more information.  Follow us at (@WWF)!
';

echo formatLinks($str);

function formatLinks($str) {
 return $str;
}

?>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
3 Solutions
 
käµfm³d 👽Commented:
It should be:

function formatLinks($str) {
  $str = preg_replace('#(https?://\S+[^\s@,.""']+)#i', '<a href="$1">$1</a>', $str);
  $str = preg_replace('#(?<!\S)(\www\.\S+[^\s@,.""']+)#i', '<a href="http://$1">$1</a>', $str);
  
  $str = preg_replace('#\w[\w\.]*\w?@[\w\.]+\w#i', '<a href="mailto:$0">$0</a>', $str);
  
  $str = preg_replace('#(?<!\S)\((@)(\S+[^\s\)]+)\)#i', '(<a href="http://twitter.com/$2">$1$2</a>)', $str);

 return $str;
}

Open in new window

0
 
Ray PaseurCommented:
@kaufmed: Please take a mulligan ;-)
http://www.laprbass.com/RAY_temp_kaufmed.php

<?php // RAY_temp_kaufmed.php
error_reporting(E_ALL);

function formatLinks($str)
{
    $str = preg_replace('#(https?://\S+[^\s@,.""']+)#i', '<a href="$1">$1</a>', $str);
    $str = preg_replace('#(?<!\S)(\www\.\S+[^\s@,.""']+)#i', '<a href="http://$1">$1</a>', $str);
    $str = preg_replace('#\w[\w\.]*\w?@[\w\.]+\w#i', '<a href="mailto:$0">$0</a>', $str);
    $str = preg_replace('#(?<!\S)\((@)(\S+[^\s\)]+)\)#i', '(<a href="http://twitter.com/$2">$1$2</a>)', $str);
    return $str;
}

// SOME TEST DATA
$text = "This is a segment of text that has www.foo.com links in it, including https://secure.net and ray.paseur@gmail.com email links";
echo PHP_EOL . $text;
echo PHP_EOL . formatLinks($text);

// SOME MORE TEST DATA
$text = 'tried doing "www.abc.com or www.bca.com" as the input text, and ';
echo PHP_EOL . $text;
echo PHP_EOL . formatLinks($text);

Open in new window

0
 
hankknightAuthor Commented:
Thanks.  There was a problem with kaufmed's code but I was able to spot it and fix it.  The working code is below:

<?php

//  Links
//  input = RegularExpressions.Regex.Replace(input, "(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")
//  input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")
//
//' Email Addresses
//  input = RegularExpressions.Regex.Replace(input, "\w[\w\.]*\w?@[\w\.]+\w", "<a href=""mailto:$0"">$0</a>")
//
//  Twitter
//  input = RegularExpressions.Regex.Replace(input, "(?<!\S)\((@)(\S+[^\s\)]+)\)", "(<a href=""http://twitter.com/$2"">$1$2</a>)")

$str = '
Hello test@example.com this is a test.  Visit www.example.com. or http://example.com for more information.  Follow us at (@WWF)!
';

echo formatLinks($str);


function formatLinks($str) {
  $str = preg_replace('#(https?://\S+[^\s@,.""]+)#i', '<a href="$1">$1</a>', $str);
  $str = preg_replace('#(?<!\S)(\www\.\S+[^\s@,.""]+)#i', '<a href="http://$1">$1</a>', $str);
  $str = preg_replace('#\w[\w\.]*\w?@[\w\.]+\w#i', '<a href="mailto:$0">$0</a>', $str);
  $str = preg_replace('#(?<!\S)\((@)(\S+[^\s\)]+)\)#i', '(<a href="http://twitter.com/$2">$1$2</a>)', $str);
  return $str;
}

?>

Open in new window

0
 
hankknightAuthor Commented:
I fixed a type the the solution that was posted.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now