Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    special string replacing...

    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.
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't fully understand your question, but i'm sure regular expressions will help. here's what i think you might mean:
    PHP Code:
    if ( preg_match("/ORDER x\d+/"$string) ) {
        
    $string "ORDER";

    "There is more than one way to do it."

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    That did exactly what i needed! Thank you!!

    If you could explain what
    PHP Code:
    /ORDER xd+/ 
    does so i can do this on my own in the future that would rock!

    Thanks for the fast help!
    If I'm postin here, I NEED YOUR HELP!!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Ultragames
    That did exactly what i needed! Thank you!!

    If you could explain what
    PHP Code:
    /ORDER xd+/ 
    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:
    PHP Code:
     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].

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    $newarray=array();
    $count count($yourarray);
    for (
    $i=1$i<= $count$i++){
       
    $newarray[] = 'ORDER x' $i;

    or if the array could also contain other elements
    PHP Code:
    $newarray=array();
    $count count($yourarray);
    for (
    $i=1$i<= $count$i++){
        if (
    substr($yourarray[$i-1], 07) == 'ORDER x'){
            
    $newarray[] = 'ORDER x' $i;
        }else{
            
    $newarray[] = $yourarray[$i-1];
        }

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    /ORDER xd+/ 
    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
    "There is more than one way to do it."

  • #7
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Best regex site around -> www.regularexpressions.info


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •