Jump to content

[SOLVED] regex any url?


keiran420

Recommended Posts

im making my own version of tinyurl.com...

 

its coming along well, kj.ueuo.com/a.php

 

But i got a problem, i want to validat the urls added,

 

only problem is that they wont nesseerily be www.somthing.com...

 

To give a better idear, here is a list of sites ppl have added to my site so far...

 

http://www.google.com

http://www.hellboundhackers.org

http://keiran420.ueuo.com/

http://www.w3schools.com

http://www.hackthissite.org

http://kj.ueuo.com/a.php

http://boardsus.playstation.com/t5/forums/postpage...

http://kj.ueuo.com/a.php?a=0n

http://facebook.com

http://www.hellboundhackers.org/profile/themasters...

http://www.hellboundhackers.org/index.php

http://forums.autosport.com/search.php?s=e1b8a4e3b...

http://www.yobt.com

http://www.google.com/search?hl=en&client=fire...

http://www.letmegooglethatforyou.com/?q=HD+Video+C...

http://www.putera.com

 

As you can see their complecated urls...

 

i basically want to check and validat any url, whatever it starts with or ends...

 

Can this be done?

(i was considering trying to access each link to see if it returned a 404 or not... tho i dont really want my site altomaticaly checking any web page sent its way either...

 

 

Link to comment
Share on other sites

<?php
function valid_url($str) {
return (!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? FALSE : TRUE;
}
?>

 

Wow, very nice, works just right it seems. :) thanks...

 

Though i do kind of feel i have just had a big spoon of food shoved into my mouth....

 

I can sort of see how this works... but some bits of it just seem random as hell to me...

 

Using this code on my site is easy, its done...

 

But understanding it. Is what i need, how the hell does this do what it does?

 

I have read a few basic tutorials on regex, and i know that it will be an invaluable tool when i learn it...

 

But somthing about those tutorials give me a head ache... >.<

 

That code gives me a headache when i try to understand each and every char of it....

 

I know i have got to do my own research, but it would be a massive help if you took that

code you have given me, and helped me understand how it works...(specially that end bit <.<)

 

Sorry for being a noob, and thanks again for the code, massive help. :)

Anything more i get from this thread will just be a much needed bonus. :)

 

Link to comment
Share on other sites

^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i

 

 

^ is an anchor meaning it must start with the pattern

(http|https|ftp) matches and captures one of the three

:\/\/ is :// literally.  (\/ is / escaped)

([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+)

 

(letter meaning a-z in the following)

 

1 letter or number

any number of letters, numbers, _ or -.

A literal dot

1 letter or number

any number of letters, numbers, _ or -.

+ means the previous pattern 1 or more times, and the grouping is around (?:\.[A-Z0-9][A-Z0-9_-]*)+, so it means that pattern any number of times.  (It allows for .co.uk for example.  Although, .blah.blah.blah.blah.blah.blah would also pass)

 

:?(\d+)?\/?/i

0-1 :

Any amount of digits

An optional literal /

Link to comment
Share on other sites

Hmm yea, i think i understand it as best as i can for now, thanks ^^

 

about my best use of regex is in a function i made.... it increments strings as case sensative values with a base value of 62...

function increment_base62($num)
{
  $count=strlen($num);
  $array=str_split($num);
  if (preg_match_all("/Z/",$num, $matches)==$count){
    $num=str_repeat('0',$count+1);
    return $num;
  }
  for ($i=$count;$i>0;$i--){ 
    if ($array[$i-1]!='Z'){ 
      switch ($array[$i-1])
      {
        case '9':
          $array[$i-1]='a'; 
          break;
        case 'z':
          $array[$i-1]='A';
          break;
        default:
          $chr=ord($array[$i-1]); 
          $array[$i-1]=chr($chr+1); 
      }
      if ($i<$count){ 
        for ($s=$i;$s<$count;$s++){ 
          $array[$s]=0; 
        }
      }
      $num=implode($array); 
      return $num;
    }
  }
  return $num; 
}

 

Works well :)

 

Well thanks for the help guys, ill check if i set it to solved, else could someone who can kindly do so please? :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.