Page 1 of 1

RegEx URLs

Posted: Mon Mar 14, 2005 9:56 am
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?

Posted: Mon Mar 14, 2005 1:37 pm
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

Posted: Mon Mar 14, 2005 1:39 pm
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

Posted: Mon Mar 14, 2005 2:39 pm
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...

Posted: Mon Mar 14, 2005 3:01 pm
by Ambush Commander
Try looking at some of the regexps here: http://www.regexlib.com/Search.aspx?k=url

They might help.