Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
preg_match() yeilds true when actually false
Old 02-05-2008, 09:10 PM preg_match() yeilds true when actually false
scrobins's Avatar
Skilled Talker

Posts: 50
Name: Stuart Robinson
Location: Busselton
Trades: 0
Hi,

I'm having trouble trying to match a string url using preg_match(). My understanding of this function is fairly limited so you may need to help explain your response.

From what I understand so far, this is the code that I'm checking;
PHP Code:
if (preg_match("/^(http:\/\/)+([a-zA-Z]+)/"$check_rss)) {
    return 
true;
}else{
    return 
false;

The $check_rss url I'm testing is http://yadayada.com@blog.

It's resulting in true when I was hoping to limit the use of symbols in the url.

Any ideas?
scrobins is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-06-2008, 12:05 AM Re: preg_match() yeilds true when actually false
Extreme Talker

Posts: 238
Location: United States
Trades: 0
I have a couple comments:
1) The + character indicates "1 or more." You are then checking for 1 or more "http://" sequences at the beginning of a string. If you are checking URLs, it's common to have a single occurrence of http://, and it's also common to have no occurrences (i.e., yahoo.com is just as valid as http://yahoo.com). A better solution would be to use a ? in that case which means "0 or 1".

2) To answer your question, your problem is caused by the lack of of the ending symbol $. Regular expressions match via a needle in a haystack method if you don't include starting and ending symbols (^ and $, respectively.) Currently, without the $, it starts at the beginning, expects to finds 1 or more "http://" right there at the beginning, then next expects to find 1 or more letters between a and z. As soon as it reaches "http://y", it has a match, but it will continue to grab up to "http://yadayada" because regular expressions are greedy by default.

3) You don't have to use the $ symbol to fix your problem, but it is a good idea, because it will allow your regexp to be as strict as possible when you finish it.

Hope that helps.
frost is offline
Reply With Quote
View Public Profile
 
Old 02-06-2008, 03:23 AM Re: preg_match() yeilds true when actually false
scrobins's Avatar
Skilled Talker

Posts: 50
Name: Stuart Robinson
Location: Busselton
Trades: 0
Yep. That did it. Thanks.
scrobins is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to preg_match() yeilds true when actually false
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29026 seconds with 12 queries