...

View Full Version : special string replacing...



Ultragames
10-20-2004, 04:05 AM
Not sure what to name this thread realy. I have a string. It might say "ORDER", or it might say something like "ORDER x2" or "ORDER x13"

How can i strip off the space, x, and the number, so i can put new ones on with a new number? I can't just change the number, because if they make the number 1, then i want to remove the space, x and number all together.

(I wouldn't even know how to change just the number)

I am pulling these strings out of an array, and then adding the numebr to them, but if the number is already there, i want to change it, so i don't end up with something like "ORDER x2 x3 x4 x8 x17" if the user can't make up their mind.

I have looked through all of PHPs string functions, and from the explinations given, i can't find anything that will do this. Please help.

Thanks for your help.

dswimboy
10-20-2004, 04:10 AM
i don't fully understand your question, but i'm sure regular expressions will help. here's what i think you might mean:


if ( preg_match("/ORDER x\d+/", $string) ) {
$string = "ORDER";
}

Ultragames
10-20-2004, 04:21 AM
That did exactly what i needed! Thank you!!

If you could explain what
/ORDER x\d+/does so i can do this on my own in the future that would rock!

Thanks for the fast help!

Fou-Lu
10-20-2004, 10:22 AM
That did exactly what i needed! Thank you!!

If you could explain what
/ORDER x\d+/does so i can do this on my own in the future that would rock!

Thanks for the fast help!

The matching technique is a perl style regular expression. It searches for the terms ORDER x.x where the second x is your digit(s). The only alteration that I would recommend to the code is:


if ( preg_match("/ORDER x?(\d+)/", $string, $match) ) {
$string = "ORDER";
}

Just the look of the original kind of says that it requires the x2 or x5 or whatever to be there. This one will allow the ORDER by itself, or the ORDER X.x. Also, if you ever needed to use your ordering number for any reason, you may call it using $match[0].

raf
10-20-2004, 10:48 AM
i don't realy understand it but since the value in your original arrays elements doesn't matter (you're stripping it of so ...), i'd think it's simply


$newarray=array();
$count = count($yourarray);
for ($i=1; $i<= $count; $i++){
$newarray[] = 'ORDER x' . $i;
}

or if the array could also contain other elements


$newarray=array();
$count = count($yourarray);
for ($i=1; $i<= $count; $i++){
if (substr($yourarray[$i-1], 0, 7) == 'ORDER x'){
$newarray[] = 'ORDER x' . $i;
}else{
$newarray[] = $yourarray[$i-1];
}
}

dswimboy
10-20-2004, 02:37 PM
/ORDER x\d+/
means to match a specific string
the "ORDER x" is fixed. \d is a special character that matches any digit. the plus sign means the previous character must occur at least once.

google for regular expressions if you want more information

trib4lmaniac
10-20-2004, 03:33 PM
Best regex site around -> www.regularexpressions.info



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum