1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development regular expressions in PHP

Discussion in 'Software' started by OneSeventeen, 8 Aug 2003.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    I want to make an if(){} statement that says if string $link starts with 'http://' then
    echo("<a href='" . $link . "'>Blah blah</a>");
    otherwise,
    echo("<a href='http://www.unm.edu/~cjadams/forms/info/" . $link . "'>Blah blah</a>");

    I just noticed not all of our info pages are stand alone pages that I can copy to their own directory, some actually branch out.

    I know this can be done with regular expressions, or maybe without, but how can I test the first seven characters of a string for "http://" ? I know there must be an easy way, right?
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    eregi() is your ultimate friend, gimme 10 mins and i'll post some code :p (if i can work it out).

    I'd go for the easier explode() method...

    Code:
    $urlparts = explode(':',$string);
    
    if($urlparts[0] == 'http') {
    
            echo [world link]
    
    }else {
    
            echo [local link]
    }
    
     
  3. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    My end result:
    PHP:
    if (!$row['InfoLink']){
        echo(
    "<td width='16'><img src='http://www.unm.edu/~cjadams/images/spacer.gif' width='16'></td>");
    } else {
        
    $info $row['InfoLink'];
        if (
    substr($info07)=="http://"){
            echo(
    "<td width='16'><a href='" $row['InfoLink'] . "' target='_Blank'><img src='http://www.unm.edu/~cjadams/images/icon_info.gif' border='0'></a></td>\n ");
        } else {
            echo(
    "<td width='16'><a href='http://www.unm.edu/~cjadams/forms/info/" $row['InfoLink'] . "' target='_Blank'><img src='http://www.unm.edu/~cjadams/images/icon_info.gif' border='0'></a></td>\n ");
        }
    }
    It works, so I'm happy :p
    Is there a shorter way than 0,7? just curious.
     
  4. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    or using strstr ...

    Code:
    if(strstr($string, 'http://')) {
    
            echo '<a href="'.$_POST[url].'">blah...</a>';
            
    } else {
    
            echo '<a href="http://richthetitch.net/'.$_POST[url].'">blah..</a>';
    }
    
    but much less reliable as it wont check that the first part is http://, it only knows if the string contains http://.
     
  5. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    In this instance it would be fine, because an http:// anywhere in a url other than at the first will mess it up anyway.

    But I'm sticking with my first way for now. I had to paste the code in like 20 places.

    I just found out I should be using a while or for loop instead of pasting the same code 6 times, with the only difference being a little bit of text and a number!

    Thanks though, I'll go with your first reccomendation when I clean my code!
     
  6. ST8

    ST8 What's a Dremel?

    Joined:
    14 Feb 2003
    Posts:
    596
    Likes Received:
    0
    you can cut those down a little more, using shorthand if statements

    Code:
    rah == 1 ? true : false;
    
    you can nest them as well

    Code:
    rah == 1 ? true
                   : rah == 2 
                   ? true
                   : false;
    
    iirc...
     
  7. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Yup, another trick I learned from linear. Isn't that the if syntax in C/#/++?
     
  8. ST8

    ST8 What's a Dremel?

    Joined:
    14 Feb 2003
    Posts:
    596
    Likes Received:
    0
    its the same as the syntax is any language ive used, seen it in c/perl/php/javascript seems pretty common. Yer granted i missed the $ off the variables, been reading about c recently :p
     
  9. sheepgoat

    sheepgoat What's a Dremel?

    Joined:
    8 Nov 2001
    Posts:
    48
    Likes Received:
    0
    if(!preg_match("/^http:\/\//i", $link)) $link = "http://".$link.


    i think that's it. i may have escaped the forward slashes wrong.


    in the php tags on this forum it shows \/ as /. why does that happen? see below

    PHP:
    \/
     

Share This Page