10 Replies - 454 Views - Last Post: 12 May 2013 - 08:58 AM Rate Topic: -----

#1 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Checking if a url is inside a variable.

Posted 12 May 2013 - 06:58 AM

Ok so i have a website and a few users are registering up to my site and then pming other users there site saying " com and join my site and mydomain.com"

So i am looking to try and check the message variable before i enter it to the other user to make sure there is no url in the message.

I am storing the message in side $message is there anyway i can use preg match to make sure that there is no .net's or .coms etc in the variable ??

I have this so far

				$message = explode(" ", $message);
foreach ($message as $word){
  if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) || (strpos($word, ".net") === 0) || (strpos($word, ".com") === 0) || (strpos($word, ".co.uk") === 0)){
 
 echo "wow it works";
 die;
  }
}



when i enter www. it catches it and shows the echo but when i add .net it lets it go though..... what am i doing wrong ?

Is This A Good Question/Topic? 0
  • +

Replies To: Checking if a url is inside a variable.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 07:19 AM

Look through the manual entry on strpos. Specifically, the part about it's return value:

php.net/strpos said:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.


Then consider what happens during your ".net" check. If you do strpos($word, ".net") and $word is, for example, example.net, then what will the function return; what is the first position of ".net" in the string? All you are doing in your condition is checking if the position of the ".net" is at position 0.

If the strpos function can't find the string in the word, it'll return a boolean FALSE, so that is what you should be checking for: Whether or not the strpos function found ".net" in the word, not whether it's the fist thing in the word.

View Postrpgmaker, on 12 May 2013 - 01:58 PM, said:

I am storing the message in side $message is there anyway i can use preg match to make sure that there is no .net's or .coms etc in the variable ??

Sure. It's fairly simple to write regular expressions that check for static variables like that. Essentially, you just put each of them into the expression, separated by the OR operator (|). For example, this would check whether you had either "apple" or "google" in your string:
/apple|google/



Note that in your case, you will be using the dot char. In a regular expression that is a special character, so you need to escape it. Like: /\.net/
Was This Post Helpful? 1
  • +
  • -

#3 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 07:24 AM

Is there any way to check muti works like .com , .net etc ??

I have check a few websites on strpos and have seen this little script

echo strpos("I love php, I love php too!","php");



So im guessing its looking for the term php ?

This post has been edited by rpgmaker: 12 May 2013 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 07:37 AM

View Postrpgmaker, on 12 May 2013 - 02:24 PM, said:

Is there any way to check muti works like .com , .net etc ??

Yes, that's what I was explaining in my last two paragraphs.

View PostAtli, on 12 May 2013 - 02:19 PM, said:

Essentially, you just put each of them into the expression, separated by the OR operator (|). For example, this would check whether you had either "apple" or "google" in your string:
/apple|google/


Was This Post Helpful? 1
  • +
  • -

#5 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 07:42 AM

would this work then ?

				$message = explode(" ", $message);
foreach ($message as $word){
  if ( (strpos($word, /\.net/ ) === 0) || (strpos($word, /\www./) === 0) || (strpos($word, /\.com/) === 0) || (strpos($word, /\.co.uk/) === 0) || (strpos($word, /\http./) === 0)){
 
 
 echo "wow it works";
  }
}

This post has been edited by Dormilich: 12 May 2013 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 07:49 AM

No, it would not.

First, strpos doesn't take regular expressions. You'd need the preg_match function you mentioned before in order to do regular expression checks. (Or one of the other preg functions.)

And second, your expressions are wrong. Mostly because you are trying to use multiple expressions, one for each TLD. The expression I demonstrated tested both "apple" and "google" in one expression. That would be the point of using a regular expression here; to avoid having to test multiple times. Otherwise strpos would do just fine.

I suggest you examine the docs for preg_match very closely. Study the examples there and see how the expressions are used there. Then apply that same technique to your code.
Was This Post Helpful? 1
  • +
  • -

#7 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 08:02 AM

Ok so i have just looked preg_math and think i get what you mean about the

/\.net/


Is this ok ? I am hoping this is right...

$line = "myurl.com  myurl.net";
if (preg_match("/\.net/\.com/\.co.uk/", $line, $match)) :
  print "Match found!";
endif;

This post has been edited by Dormilich: 12 May 2013 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 08:10 AM

That's very close. The only thing you did wrong there was using / instead of | to separate the TLDs. Remember, like I did in my original example: /apple|google/, or in your case: /\.com|\.net/

One other inconsequential note: That expression can also be shortened slightly, using regexp grouping: /\.(com|net)/. Not really necessary though.

Regular expressions are very powerful. Well worth investing some time to study them.

Edit:
One thing I just noticed. If you are going to check for .co.uk, remember to escape the dot between the "co" and "uk" as well. In a regular expression, an unescaped dot character is read as "any character". So without escaping it: /\.co.uk/ would match the domain all right, but it would also match any other charter, like "comuk", or "coruk". (Couldn't think of any actual words that matched that :P)

This post has been edited by Atli: 12 May 2013 - 08:13 AM

Was This Post Helpful? 1
  • +
  • -

#9 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 08:13 AM

So
/\.(com|net|co.uk|www.|http)/



Would work has well ? Look much easier to understand like that
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 08:22 AM

The "www" and "http" would not belong in that group. If you group them with the TLDs, you would be checking for .www. and .http. They need to be separated from them if they are to work properly.

Like with the TLDs, you can separate those from the TLDs.
/http|www\.|\.(com|net|co\.uk)/


Any time you see a |, that means "OR". So, that expression is essentially saying: "Check for 'http', or 'www.', or a dot followed by 'com', 'net' or 'co.uk'"

Also, don't forget to escape the dot characters! That's very important.
Was This Post Helpful? 1
  • +
  • -

#11 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Checking if a url is inside a variable.

Posted 12 May 2013 - 08:58 AM

Thank you so much works like a dream. A big thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1