Jump to content

ereg_replace to preg_replace? ( ereg depricated )


Recommended Posts

Hi Guys,

I have the following code that uses ereg however im useless when it comes to regex so I dont know how to move to preg ..

 

Essentially, It takes a string and makes a URL "clickable" ie: if it finds a link like http://www.google.com it would change http://www.google.com to a proper html href

 

Could someone help convert this to preg so my code is more compatible with newer version of PHP..

 

Thanks muchly in advance.

Graham


$message = "Have you seen google? http://www.google.com ";
$message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]"," :link: <a title=\"Click to open in a new window\" alt=\"Click to open in a new window\" target=\"blank\" href=\"\\0\">\\0</a>", $message);

Here's a discussion with myself as I went down this path. Go to the last post for a preg replace that works on a random url link ;D

 

http://www.phpfreaks.com/forums/index.php?topic=336117.0

(You don't need parts 1-4 as they deal with url links enclosed in bb code)

 

You will also need the following line after prt5;

$replaceUrlPrt6 = preg_replace("/^(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a class=\"nWhite\" href=\"http://\\2\" target=\"_blank\">\\2</a>", $replaceUrlPrt5);

 

This is so that you can have addresses that start with or without http:// or with or without www. but need at least one or the other

Thanks very much for your reply.

 

It doesn't seem to do anything? ( it doesn't seem to replace anything and no errors in logs.. )

 

<html><body>
<?php

$message = "Why not visit www.google.com or google.com or http://www.google.com or http://google.com or www.google.co.uk or http://google.co.uk ";


$message = preg_replace("/^(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message);

echo $message;

?>

</body>
</html>

Oh wow, glad you spotted that! I thought I had it all sorted for my forum, I clearly hadn't done a thorough testing. The following almost works except for the second part runs the regex again on the third hyperlink. I'm not sure why this is and am going to have to sort this out later. Replacing just google.com does not work (and I don't think it does in many forums) but could possibly be added by searching for a mixture of characters that finishes with .com or .co.uk etc.. I'll be working on this regex tonight so I'll let you know when it is properly working. What I did so far was to remove the caret which was making it only look for links at the beginning of the block of text.

 

<?php

$message = "Why not visit www.google.com or google.com or http://www.google.com or http://google.com or www.google.co.uk or http://google.co.uk ";

$message = preg_replace("/(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $message);
$message = preg_replace("/(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message);

echo $message;

?>

Although, I personally didnt use message1 and message2....

 

The below should work anyway without the need to generate new strings.:

$message = preg_replace("/(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $message);

$message = preg_replace("/(?<!http:\/\/)(?<!https:\/\/)((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $message);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.