Page 1 of 1

Simple RegEx, difficult for me :(

Posted: Wed Feb 01, 2006 6:55 am
by mzfp2
Just looing to do the following :

Extract the Page Number from a url like the following :

http://localhost/Lifestyle_And_Dating/Dating/Page_1

POssible values for page arre ... Page_1 to Page_99.

After extracting this from the url and storing it in a variable, I want to remove it from the source sttring (url)), so I am just left with

http://localhost/Lifestyle_And_Dating/Dating/

Many Thanks

Posted: Wed Feb 01, 2006 7:39 am
by raghavan20

Code: Select all

<?php
$input = "http://localhost/Lifestyle_And_Dating/Dating/Page_1";
$matches = preg_split("/\/(Page_)/", $input);
print_r($matches)
?>

Code: Select all

Array
(
    [0] => http://localhost/Lifestyle_And_Dating/Dating
    [1] => 1
)

Posted: Wed Feb 01, 2006 10:08 am
by pickle
You don't need regex for this.

Code: Select all

<?
$url = "http://localhost/Lifestyle_And_Dating/Dating/Page_1";
$stripped_url = dirname($url);
?>
dirname()

Posted: Wed Feb 01, 2006 10:10 am
by raghavan20
pickle wrote:You don't need regex for this.

Code: Select all

<?
$url = "http://localhost/Lifestyle_And_Dating/Dating/Page_1";
$stripped_url = dirname($url);
?>
dirname()
probably he needs the page number for some purpose as well...

Posted: Wed Feb 01, 2006 10:40 am
by pickle
raghavan20 wrote:probably he needs the page number for some purpose as well...
You're 100% right. Had I fully read the question I would have noticed that :missing emoticon for d'oh!::wink:.

As that's the case, you can use basename() to get the file name. Though I can see now that regex may be simpler to some(I wouldn't use it here, but that's just me).