Viktor Kulikov
Viktor Kulikov

Reputation: 269

Regular URL parsing does not work correct with @ symbol

I have this regular exception:

(?:([^\:]*)\:\/\/)?(?:([^\:\@]*)(?:\:([^\@]*))?\@)?(?:([^\/\:]*)\.(?=[^\.\/\:]*\.[^\.\/\:]*))?([^\.\/\:]*)(?:\.([^\/\.\:]*))?(?:\:([0-9]*))?(\/[^\?#]*(?=.*?\/)\/)?([^\?#]*)?(?:\?([^#]*))?(?:#(.*))?

1) If parse url: http://test.ru/long/path/file.ext?queryparam=123&more=234 - its OK

http://viper-7.com/QNCAQu

2) If parse url: ftp://admin:[email protected]/long/path/file.ext - its OK

http://viper-7.com/jkQHpP

3) BUT, if in GET parameter exist [AT] (@), it is not work correct!

Example: http://test.ru/long/path/file.ext?queryparam=123&[email protected]

http://viper-7.com/9qIdcj

PS. Regular exception find on stackoverflow, but don't remember the topic.:(

Upvotes: 0

Views: 174

Answers (2)

kittycat
kittycat

Reputation: 15045

If you are trying to parse a URL, regex is not the way. PHP has a built in function called parse_url() that will break the URL up into its segments to easily access. More efficient and way less code. In addition you can further break up the query segment with parse_str() Lastly, if you want to break up the path you can use pathinfo() as well. PHP has all these readily available, you just need to start using them.

Upvotes: 2

Tschallacka
Tschallacka

Reputation: 28712

That is correct that it fails because it's a malformed url.

The url should read

http://test.ru/long/path/file.ext?queryparam=123&mail=test%40domain.com

instead of the malformed [email protected].

the javascript function encodeURIComponent('@'); is your friend :D

Upvotes: 3

Related Questions