-
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]";
?>
-
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
alone so that it will be flexible?
-
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;
-
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;
-
Looks like that should work.
-
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?
-
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.
-
In that case it looks like what fastsol1 is suggesting would be better. I hope it helps.