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

x
?
Solved

php makelinks function - small problem

Posted on 2007-07-25
9
Medium Priority
?
241 Views
Last Modified: 2008-02-01
Hi,

I need help with this php regex function that makes links out of normal html.

      function makeLinks($sourceText) {
            $destText = preg_replace( "/([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})/", '<a href="mailto:\\0">\\0</a>',$sourceText);
            $destText = preg_replace_callback('/\bhttp[^\s]+/',create_function('$matches', 'return "<a target=\"_blank\" rel=\"nofollow external\" href=\"$matches[0]\">" . preg_replace("#(\.|/)#", "&shy;$1", $matches[0]) . "</a>";'),$destText);
            return $destText;
      }

The problem I have is that when the link is followed by anything else than a whitespace, it gets included in the link ie: <p>my homepage is http://www.nellen.co.za</p>  Then the </p> gets included in the link...

Any help will be greatly appreciated.
0
Comment
Question by:nelwa
  • 6
  • 3
9 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 19563940
Can you give us an example text to work with please.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 19563945
You could try replacing ...

'/\bhttp[^\s]+/'

with ...

'/\bhttp[^\s<]+/'

This changes ...

http and everything upto a whitespace

into

http and everything upto a whitespace or a <

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 19563954
Using ...

echo makeLinks('this is http://www.bob.com<b>bold</b>');


Before the change ...

this is <a target="_blank" rel="nofollow external" href="http://www.bob.com<b>bold</b>">http:­/­/www­.bob­.com<b>bold<&shy;/b></a>

after the change ...

this is <a target="_blank" rel="nofollow external" href="http://www.bob.com">http:­/­/www­.bob­.com</a><b>bold</b>


Which is what I think you are asking.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 19563966
I would also add comma ...

I would also deal with dot followed by whitespace, comma or < - working on now to do that RIGHT now.
0
 
LVL 1

Author Comment

by:nelwa
ID: 19563967
Ok, here follows an example.

$text = "<p>My homepage is http://www.nellen.co.za/</p>";

$text = makeLinks($text);

echo $text;  //gives:  <p>My homepage is <a href="http://www.nellen.co.za" target="_blank" rel="nofollow external">http://www.nellen.co.za</p></a>

The problem is that the </p> is included in the link text... so what I basically need is that the regex is altered to check correctly when the url ends.

A url should thus end when it encounters:
- any non url characters like < for instance
- or a . or , which is followed by whitespace
- or a whitespace
0
 
LVL 1

Author Comment

by:nelwa
ID: 19563977
ok, it seems that while I was typing that example, three answers were added, so please just add the , and . problems into the regex for me...

Thanks
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 19564020
<?php
function makeLinks($sourceText)
      {
      $destText = preg_replace( "/([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})/", '<a href="mailto:\\0">\\0</a>',$sourceText);
      $destText = preg_replace_callback('`\bhttp://((?!\.\s)[/a-z0-9-\._])+`i',create_function('$matches', 'return "<a target=\"_blank\" rel=\"nofollow external\" href=\"$matches[0]\">" . preg_replace("#(\.|/)#", "&shy;$1", $matches[0]) . "</a>";'),$destText);
      return $destText;
      }

echo makeLinks('<p>My homepage is http://www.nellen.co.za/</p>'), PHP_EOL;
echo makeLinks('<p>My homepage is http://www.nellen.co.za/, but only on a monday</p>'), PHP_EOL;
echo makeLinks('<p>My homepage is http://www.nellen.co.za/. Se you soon</p>'), PHP_EOL;
echo makeLinks('<p>My homepage is http://www.nellen.co.za/ - it is a nice place</p>'), PHP_EOL;
?>

outputs ...

<p>My homepage is <a target="_blank" rel="nofollow external" href="http://www.nellen.co.za/">http:­/­/www­.nellen­.co­.za­/</a></p>
<p>My homepage is <a target="_blank" rel="nofollow external" href="http://www.nellen.co.za/">http:­/­/www­.nellen­.co­.za­/</a>, but only on a monday</p>
<p>My homepage is <a target="_blank" rel="nofollow external" href="http://www.nellen.co.za/">http:­/­/www­.nellen­.co­.za­/</a>. Se you soon</p>
<p>My homepage is <a target="_blank" rel="nofollow external" href="http://www.nellen.co.za/">http:­/­/www­.nellen­.co­.za­/</a> - it is a nice place</p>

which seems to cover what is required.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 19564025
If you find more instances where it is not working come back and I'll update the regex.
0
 
LVL 1

Author Comment

by:nelwa
ID: 19564060
thanks...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question