|
Posted by Carl on 11/03/07 18:27
Salve Håkedal <ikkjespam.salve@slogedalen.no> writes:
> On 2007-11-02, Carl <c.groner@gmail.com> wrote:
>> "Sanders Kaufman" <bucky@kaufman.net> writes:
>>> Does it make your eyes and ears bleed,the way it does mine?
>>> Been doin this stuff since the 70's - but regex still makes me cry.
>>>
>>> On that note, I am at the moment, writing a function that could sure benefit
>>> from some regex.
>>> I just want to see if a string starts with "http(s)://", "news:", "mailto:",
>>> "ftp:".
>>> That's a pretty simple regex, right?
>>> Hoooowwww?
>>
>> This should get you started:
>>
>> $pattern = '/^(http(s)?:\/\/|news:|mailto:|ftp:)/';
>>
>> $tests = array('http://www.google.com', 'https://www.google.com',
>> 'news:comp.lang', 'mailto:test@nowhere.com',
>> ' http://www.google.com',
>> 'bad_http://www.google.com',
>> 'mailtobad:fdsa', 'ftp://ftp.host.net',
>> 'ftpbad:', 'badftp://');
>>
>> foreach($tests as $v) {
>> print "'".$v."'" . ' ~ ' .
>> (preg_match($pattern, $v)
>> ? 'matches'
>> : 'doesn\'t match')."\n";
>> }
>
> Thank you, Carl, for the script.
>
> But the regexp there is as simple at I could have written myself. What
> I need ...
--8<-- message cut -->8--
Salve,
My response was a followup to Sanders, not your O.P., I assumed that
the previous posts answered you question already.
The question you posted is quite common, and google'n should turn
up enough examples that you should'nt have to do much to get it working
decently enough.
For starters, this looks promising, though I haven't tested it (from
the 1st results page of a google search)
http://immike.net/blog/2007/04/06/5-regular-expressions-every-web-programmer-should-know/
--
Hope that helps,
Carl.
[Back to original message]
|