Page 1 of 1
ereg_replace = urls and emails from a string
Posted: Mon Sep 22, 2003 4:05 pm
by kendall
Hello,
I am trying to strip inputted memo type data of url's and email address...
i have created the following expression
Code: Select all
exp = "^((їa-zA-Z\/\/:]+)|(їa-zA-Z0-9\.@]+))\.їa-zA-Z\.]{1,3}$";
$desc = eregi_replace($exp,"",$_POSTї'description']);
yet i dont seem to be yielding any results with it...
to me i think i have tried to theorise the expression from what i wanted to accomplish
^(([a-zA-Z\/\/:]+) means im trying to check if it contains the url format
|([a-zA-Z0-9\.@]+)) means im checking for either the www or the email syntax
\.[a-zA-Z\.]{1,3}$ means im cheking for the extension
but the dog gone thing doesn't seem to work...have i gotten my rationale rite??
how does this work in reverse?
Kendall
Posted: Tue Sep 23, 2003 5:21 am
by delorian
First of all, don't use POSIX regular expresions but PCRE (Perl-comaptibile).
Perl regular expressions are faster and not so greedy as POSIX.
Secondly, you have an error with ^ and $ marks. As I understood, you are searching some string for the url or mail address, right

If so, the string could be something like this: "Some stuff
http://www.wp.pl some stuff" - so you can't search at the begining of the text using ^ and you can't search at the end of the string using $, right.
Ok, but if you would like to change your regulars into PCRE-like use the following patterns to find:
an e-mail addres:
Code: Select all
"|^ї\w\-]+(\.ї\w\-]+)*@ї\w\-]+(\.ї\w\-]+)*(\.ї\w]{2,4})+$|i"
url:
Code: Select all
(((http|https)://ї\w\-]+ї\w\-\.]+ї\w/\.\?\=\&]*))i
and the functions:
http://php.net/manual/en/ref.pcre.php
It should do the trick.

Posted: Tue Sep 23, 2003 7:53 am
by kendall
Yo, thanks....i didnt no they was a PCRE thing...i will look it up...
Kendall
PCRE Regular Expression
Posted: Tue Sep 23, 2003 8:11 am
by kendall
delorian,
thanks for your input....i have been loooking it up and i hjave a question to ask....but first
to understand the expression
Code: Select all
"|^ї\w\-]+(\.ї\w\-]+)*@ї\w\-]+(\.ї\w\-]+)*(\.ї\w]{2,4})+$|i"
the : "|" is the delimiter
the : "[\w\-]+(\.[\w\-]+)*@"
match words characters,-, repeated more than 0/ 1 or more times with a @ symbol
the : "[\w\-]+(\.[\w\-]+)*(\.[\w]{2,4})"
match word characters ,-, repeated 0/ 1 or more times after the @
i kinda got confused as to why you would wanna
(\.[\w\-]+)
but if my theory is corect but this is to match sub domain types? i.e.
emailaddress@miss.you.com. the latter part
(\.[\w]{2,4}) checks for the extension including the co.tt type extensions rite?
what confuses me is the i? what is the i?
Kendall
Posted: Tue Sep 23, 2003 8:26 am
by JayBird
doesn't the i mean case-insensitive?
Mark
POSIX regular expression matching urls and emails n a string
Posted: Tue Sep 23, 2003 8:32 am
by kendall
Just to update you...
I have looked over my expression build and got this to work
Code: Select all
//expression that strips string of url and email address <br> "((їa-zA-Z\/\/:]+)|(їa-zA-Z0-9\-\_.@]+))\.їa-zA-Z0-9\.]{1,3}ї?=A-Za-z0-9]+"
anybody wanna have a go at it...let me know where the down fall meh lie?
question...? how can i use this when i want to match urls to make them linkable? add tags to a url? i.e.
reg_replace() = <a href= </a>
kinda waht this forum does
Kendall
Posted: Tue Sep 23, 2003 10:25 am
by kendall
Just to update people,
i was able to develop this expression that help strip urls' and email addresses from strings
Code: Select all
$exp = "`(((ї\w\-\.]+)*://)|(ї\w\-\.]+@)|(www\.))((ї\.\w\-]+)(ї\w\-\.]+ї\w/\.\?\=\&]*))`i";
let me know how it works out....feedback are welcome
Kendall