RegEx URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

RegEx URLs

Post by zenabi »

I'm using preg_replace() to replace URLs (domain names) with ***:

Code: Select all

$pattern = "/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}/i";

$string = "My URL is example.co.uk. Please visit my site."
echo preg_replace($pattern, "***", $string);
//Outputs: My URL is ***. Please visit my site.

//But if there is no space after the end of the first sentence
$string = "My URL is example.co.uk.Please visit my site."
echo preg_replace($pattern, "***", $string);
//Outputs: My URL is *** visit my site.
What can I do to my regular expression so it keeps the start of the second sentence?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmmm... this is a trciky one :?

By the way... when you use the 'i' modifier theres no need to do [a-zA-Z..

Just [a-z... will do since it's not case sensitive.

I'll think about this. I like my regexp ones :P
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

This is slightly better but now it chops off the period/full-stop if there is a space. Doesn't take words out though. Must be nearly there.

Code: Select all

<?php

$pattern = "/[a-z0-9\-\.]+\.[a-z]{2,6}(\.)/i";
 
$string = "My URL is example.co.uk. Please visit my site.";
echo preg_replace($pattern, "***", $string).'<p>';
//Outputs: My URL is ***. Please visit my site.
 
//But if there is no space after the end of the first sentence
$string = "My URL is example.co.uk.Please visit my site.";
echo preg_replace($pattern, "***$1", $string);
//Outputs: My URL is *** visit my site.

?>
EDIT:

Sorry that was right, it does work perfect... could just be written a bit better.

Code: Select all

$pattern = "/[a-z0-9\-\.]+\.[a-z]{2,6}\./i";
 
$string = "My URL is example.co.uk. Please visit my site.";
echo preg_replace($pattern, "***.", $string);
:-D
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

Thanks d11. Like you said it kinda works...but now it replaces "example.co.uk" with "***uk" and doesn't replace "example.com" at all...

I'm not an expert on regexps at all. I tried something like this:

Code: Select all

$pattern = "/[a-z0-9\-\.]+(\.[a-z]{2,6}){1,2}/i";
but it doesn't work. I hope you can see what I was trying to do...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Try looking at some of the regexps here: http://www.regexlib.com/Search.aspx?k=url

They might help.
Post Reply