Hi everyone, this is my first thread in phpbuilder 🙂
and like u are guessing i have a doubt :p

i have the following statement:

$url = "http://someurl.com";
$replace = strtolower(str_replace("http://","",$url));

echo $replace;

$url will be sent from a form...
well, i would like to clean up this url like:
Your website:
[text field]http://mywebsite.com[/text field]

ok, the str_replace function will work...
but if someone say
[text field]http//mywebsite.com[/text field]
it won't work..

so how can i handle with this?

    Besides giving examples, state what you want to do.

      I think he wants to strip the http bit when he puts it in the database or something so when he adds the http:// bit again it will be a proper link.

      If someone types http//mywebsite.com id will end up as http://http//mywebsite.com.

      <?php
      $regex = '/([a-zA-Z:]+)\/{1,6}/'; // picked {1,6} /,////// can be 3 or 4  just as an example due to if we counting on people being wrong, might as well count on them being really wrong :)
      $url  = 'http://www.someurl.com/monkeys/index.htm';
      
      preg_match( $regex , $url , $matches );
      print_r($matches);
      
      echo preg_replace( $regex ,"", $url   ,1);
      ?>
      

      due to there are two // the chances of one getting there is reasonably high.
      Probably some ninja regex out there that would burn my eyes would be better but I am not that good with them.

      Mine doesn't deal with

      http//:www.someurl.com/monkeys/index.htm

        another way would be to pick out only
        xxxxxxxxx.xx(x) and xxxxxx.xxxxx.xx(x) www.xxxxx.xx(x) etc etc patterns
        and delete anything else
        that is, extract the domain name

          halojoy wrote:

          another way would be to pick out only
          xxxxxxxxx.xx(x) and xxxxxx.xxxxx.xx(x) www.xxxxx.xx(x) etc etc patterns
          and delete anything else
          that is, extract the domain name

          That wouldn't handle any specific file, then, just the root level domain name, which screws out lots of users on free hosting.

            There also comes a point where you can only guess so much before you simply have settle for the fact that if people are too ignorant and/or careless to type a URL correctly, then they deserve to have their data mangled into a heap of useless garbage.

              Write a Reply...