regex for this simple string please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

regex for this simple string please

Post by mjseaden »

Hi there,

I have a script called sitemap_ (that's right, sitemap with _ on the end and no extension).

I am detecting any parameter (REQUEST_URI) after the sitemap_ either in the form

sitemap_X
sitemap_X_Y

can anyone tell me what regex I use to determine whether it is of either type, and if so, what X and Y are in the string?

Many thanks

Mark
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

You don't necassarily need to use regex.

You could probably use one of these functions:

split
explode
substr
strlen

Or a comination thereof to get X or Y, if I understand the question correctly.

e.g.

Code: Select all

<?php
$url = "blah_x_y";

$breakup = explode("_",$url); // turns $breakup into an array ("blah","x","y")

$default = $breakup[0]; // blah
$x = $breakup[1]; // x
$y = $breakup[2]; // y
?>
Just a simple non generic example of how you can manipulate strings.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes I agree. I'm all for RegExp's but in this instance, I'd go with explode().

That way it's clearer to see what you have extracted by just looping over your obtained array with foreach(), since there could be any number of arguments in the string. :wink:
Post Reply