![]() Equipping You to Communicate Effectively | support CMN & share a library of 19K+ images, videos, etc Go Pro! |
![]() | ![]() |
| |||||||
| Website Coding Discussions PHP, MySQL, Java, Javascript, ASP, etc. |
![]() |
| | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| |||
| Anybody familiar with regex, please keep reading: On one of my databases the external links are saved in this format: Code: [url=www.google.com/index/]Link[/url] Code: $table_field = preg_replace( "/\[url=www\.([a-z0-9\-]+\.(com|org|net|us|eu|fi))(\/[a-zA-Z0-9\-\_\/]+)(\.(jpg|gif))?\](.+?)\[\/url\]/", "<a href='http://www.$1$3$4'>$6</a>", $table_field); |
| ||||
| I'm not much good with php but I like regular expressions a lot. I analyzed your regex and I think I see the problem. I'm assuming * means "zero or more" in php regex. \[url=www\.([a-z0-9\-]+\.(com|org|net|us|eu|fi))(\/[a-zA-Z0-9\-\_\/]+)*(\.(jpg|gif))?\](.+?)\[\/url\] What you were doing is saying that you always require at least one / followed by some various characters. In the case of the last URL you posted, there was no / followed by extra characters. So by adding the * after (\/[a-zA-Z0-9\-\_\/]+) it made it optional. Now with all of that said, I don't believe the pattern you are using will properly validate all URLs. Even with the * to solve one problem, this URL won't validate: java.sun.com Have a look at the specification at http://www.w3.org/Addressing/rfc1738.txt and you'll be surprised how much simpler you could make your regex.
__________________ Sanctus Software More RegEx: (?<BookTitle>[A-Za-z0-9 ]+)\s(?<ChapterNumber>\d{1,3})[:](?<VerseNumber>\d{1,3}) |
| ||||
| Here's perhaps something to have a look at: PHP Code: http://sun.java.com:90/dir1/dir2/dir3/pic.jpg http://sun.java.com:90/dir1/dir2/dir3 http://sun.java.com:90/ http://sun.java.com ftp://downloads.somesite.org http://sun.java.com/special%0Fcaracters So it supports: - capturing groups... feel free to strip them out if you don't need them - protocol (http, ftp) optionally - any number of labels in the host name (but not imposing length limits) - port numbers optionally - path including special characters (%xx) where "x" is a hex digit. I didn't do query strings. Also, this is compliant with .NET (C#, VB, ASP.NET) so you might need to add some escape characters for php.
__________________ Sanctus Software More RegEx: (?<BookTitle>[A-Za-z0-9 ]+)\s(?<ChapterNumber>\d{1,3})[:](?<VerseNumber>\d{1,3}) |
| |||
| Thanks Shayward! Honestly I was not expecting much of an answer from this forum, but I'm lazy to sign up for new forums, so I gave it a shot...fortunately! I'm intentionally avoiding any sun.java.com or :90 -type urls and especially any ftp urls. All I'm allowing is the basic www.java.com or with some subdirs and with/without jpg|gif file. Nothing else. But thanks a thousands for your input! I hope that * would solve the problem. I'll give it a shot in the morning. |
![]() |
|
| Tags |
| php, preg_replace, problem, regex |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |
Register Now for FREE! | |||||
| |