Simple RegEx, difficult for me :(

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Simple RegEx, difficult for me :(

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply