get stuck with Regualer Expression

Discussion in 'PHP' started by goldensea80, Jan 11, 2005.

  1. #1
    I am not good at Regular Expression,. I am trying the following:
    Change the line1 to line2:
    Line1: "<a href='sub1/mypage.php'>MyPage</a>"
    line2: "<a href='http://mydomain.com/sub1/mypage.php'>MyPage</a>"

    Of couse, if line2 stay the same when line 1 is:
    "<a href='http://something.com/sub1/mypage.php'>MyPage</a>"

    The point is: line2 shouldn't change if there is http:// or ftp:// or anything else!
    Please help if you have clue!
     
    goldensea80, Jan 11, 2005 IP
  2. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    
    if (strpos("$url", "://") !== false)
    {
       // the url contains a ://
    }
    else
    {
       str_replace("sub1","http://yourdomain.com/sub1",$url);
    }
    
    
    PHP:
    Maybe start with something like this.
     
    rvarcher, Jan 11, 2005 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Thanks, however, maybe I still have to use eregi, because, "sub1" may be any name.
     
    goldensea80, Jan 13, 2005 IP
  4. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Further more, my problem relates to redirect URL, for example, with absolute URL:

    $url="http://bh2.host.sk/"; // Or just any ABSOLUTE URL
    $redirect="http://mydomain.com/mycript.php";
    $link1="This is the link <a href=\"http://bh2.host.sk/sub1/PHP.doc\">The document </a>";
    echo "\n<br>1.1 ".eregi_replace("href=\"([a-z]+://.+)\"","href=\"$redirect?url=\\1\"",$link1);
    PHP:
    But I meet problem with RELATIVE URL, for example if link2
    $link1="This is the link <a href=\"sub1/PHP.doc\">The document </a>";
    $domain="http://bh2.host.sk"
    PHP:
    I need to have a result something like:
    http://mydomain.com/myscript.php?url=http://bh2.host.sk/sub1/PHP.doc
    Code (markup):
     
    goldensea80, Jan 13, 2005 IP