Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Want to change a url

  1. #11
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,718
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    The following does the same thing as the code I posted earlier, but has been simplified somewhat.

    PHP Code:
    <?php
    $test
    ="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
    preg_match('/\/{1}[^\/]*?\/{1}(?!.*?\/).*/',$test$extract);
    echo 
    "http://www.inchicago.com$extract[0]";
    ?>
    EDIT: and simplified a little further:

    PHP Code:
    <?php
    $test
    ="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
    preg_match('/\/[^\/]*?\/(?!.*?\/).*/',$test$extract);
    echo 
    "http://www.inchicago.com$extract[0]";
    ?>
    Last edited by james438; 02-17-2013 at 04:40 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #12
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    I am having a bit of trouble with this. I am a beginner with regular expressions, so please bear with me. What I am trying to do is match
    Code:
    http://www.mytravelsite.com/states/cities/thiscity/
    to
    Code:
    http://www.inchicago.com/
    so that when I pass the url through my object

    Code:
    <a href="http://www.mytravelsite.com/states/cities/thiscity/dir0-6/whateverpageisindexed.shtml">title</a>
    becomes a clickable link to thats file location that looks like

    Code:
    <a href="http://www.inchicago.com/dir0-6[whicheverisreturned]/whateverpageisindexed.shtml">title</a>
    The code
    Code:
    $test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml"; 
    preg_match('/\/[^\/]*?\/(?!.*?\/).*/',$test, $extract); 
    echo "http://www.inchicago.com$extract[0]";
    returns

    Code:
    http://www.inchicago.com/0/apage.shtml
    but the numbered dir may fluctuate as well as the returned page. Hopefully that makes sense. How can I make

    Code:
    http://www.mytravelsite.com/states/cities/thiscity/
    resolve to

    Code:
    http://www.inchicago.com/
    but leave

    Code:
    /0/apage.shtml
    alone so that it will be flexible?

  3. #13
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Will the grabbed url always be this in the front of the url?- http://www.mytravelsite.com/states/cities/thiscity/
    If that is a static part of the url then you could use a str_replace() on that part and set it to nothing so you are left with the last part of the url.
    PHP Code:
    $url "http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
    $new_url str_replace("http://www.mytravelsite.com/states/cities/thiscity/"""$url);
    echo 
    $new_url

  4. #14
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    So to make it
    Code:
    http://www.inchicago.com/dir0-6/somepage.shtml
    Would I do this?

    Code:
    $url = "http://www.mytravelsite.com/states/cities/thiscity/"; 
    $new_url = str_replace("http://www.mytravelsite.com/states/cities/thiscity/", "http://www.inchicago.com/", $url); 
    $endurl = $passedurldata;
    echo $new_url.$endurl;
    Last edited by itivae; 02-20-2013 at 05:02 PM.

  5. #15
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Looks like that should work.

  6. #16
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,718
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    PHP Code:
    <?php
    $test
    ="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
    preg_match('/\/{1}[^\/]*?\/{1}(?!.*?\/).*[^\/]$/',$test$extract);
    if (
    $extract[0]==""$prefix="http://www.inchicago.com/";
    else 
    $prefix="http://www.inchicago.com";
    echo 
    "$prefix$extract[0]";
    ?>
    I modified it just a little to account for the possibility that no file name is listed. This will now match

    Code:
    http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml
    http://www.mytravelsite.com/states/cities/thiscity/
    http://www.mytravelsite.com/states/cities/thiscity/27/apage.shtml
    http://www.anysite.com/stage/theater/0who/apage.shtml
    I do not think I fully understand what you are asking for.

    How does my earlier example not give you what you are looking for?
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #17
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    Hi James,

    Basically there are several hundred indexed items. Since the beginning of the url

    Code:
    http://www.mytravelsite.com/states/cities/thiscity/
    needs to be equated to

    Code:
    http://www.inchicago.com/
    always.

    but the directories 0-6 come after and have several hundred .shtml pages in each.

    So I really need to match those urls but leave the last two /dirs/ i.e.(/0/apage.shtml/ might be /1/apage23.shtml) as however they are served back from the query.

    Hopefully that is clearer.

  8. #18
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,718
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    In that case it looks like what fastsol1 is suggesting would be better. I hope it helps.
    To choose the lesser of two evils is still to choose evil. My personal site

Similar Threads

  1. ajaxcars.zip how to change url on change of div
    By shahgm in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-21-2011, 01:51 AM
  2. on change(dropdown) change 2 div
    By emanuelle in forum JavaScript
    Replies: 1
    Last Post: 06-20-2010, 10:11 AM
  3. Html change on click, change to another html when clicked again
    By ShiWenBin in forum Looking for such a script or service
    Replies: 1
    Last Post: 11-04-2007, 01:11 AM
  4. name change?
    By boogyman in forum The lounge
    Replies: 12
    Last Post: 05-20-2007, 11:24 PM
  5. Change Var
    By Tabo in forum JavaScript
    Replies: 4
    Last Post: 04-24-2007, 09:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •