• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2455
  • Last Modified:

remove HTML tags from href="" values

i have this script to find website addresses in a  string and wrap <a> tags around it.  however if the website address is at the end of the string it appends the </p> tags to the href="" value like this:

<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>
<p><a href="http://site.com/contact.html</p>" title="">http://site.com/contact.html</p></a>

my code:

<?php
function fcnConvertWebsiteAddress($data)
{
      $data = preg_replace('/(https?\:\/\/\S*)/','<a href="${1}" title="">${1}</a>',$data);
      return preg_replace('/(?<![\'"])([^\/])(www\.[^\s\,"]+)/','$1<a href="$2" title="">$2</a> ', $data);
}

$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";

$data = fcnConvertWebsiteAddress($data);

echo $data;
?>
0
ellandrd
Asked:
ellandrd
  • 2
  • 2
  • 2
  • +1
1 Solution
 
administradoresCommented:
<?php

$data = '<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>';

echo strip_tags($data);
?>
0
 
administradoresCommented:
Also the html code hasn't the right format.

<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>

should look:

<p><a href="http://www.site.com" title="">http://www.site.com</a></p>
0
 
GEM100Commented:
administradores: he needs to do the opposite.

ellandrd: this code will work with <p></p> in original string, and without:

<?php
function fcnConvertWebsiteAddress($data)
{
      $data = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" title="">$1</a>', $data);
      return $data;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($data);
echo $data;
?>
0
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.

 
xberryCommented:
Why not simply like that:

<?php
function fcnConvertWebsiteAddress($data)
{
      $data = strip_tags($data);
      $data1 = preg_replace('/(https?\:\/\/\S*)/','<p><a href="$1" title="">$1</a><p>', $data);
      return $data1;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($data);

echo $data;
?>
0
 
xberryCommented:
excuse GEM100, obviously crossposted at same time as you did.
Definitely your solution is more elegant ; )
0
 
ellandrdAuthor Commented:
GEM100

you solution doesnt work for formats such as www.site.com.  it will only work if the website address has http:// in it...
0
 
GEM100Commented:
ellandrd: I agree, it matches URLs of  proper format. There can be many scenarios to match, BUT If your format is always having URLs in <p></p> tags, you can use this:

<?php
function fcnConvertWebsiteAddress($data)
{
      $data = preg_replace('@<p>(http(s)?)?(://)?(.*?)</p>@', '<p><a href="http$2://$4" title="">$1$3$4</a></p>', $data);
      return $data;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>www.site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($data);
echo $data;
?>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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