Remove numbers from words and work with the numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webandwe
    New Member
    • Oct 2006
    • 142

    Remove numbers from words and work with the numbers

    Hi.

    Say I have the following id. webandwe13

    Is there I way I can remove the 13 to work with it and add it back when I'm done.

    I have a profile program and when the user upload I want to increase the number but don't know how to remove the number to work with it.

    Is it even possible?

    Thanks

    Webandwe
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there are various ways. you can for instance use a Regular Expression to remove and get number as well as cutting it off with string functions (if there are length constraints on either the number or the letter part).

    adding the number is easy:
    [PHP]$your_string = $letter_part . $number;[/PHP]

    regards

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Why not do yourself a favour and store the number in a database. Takes away the hassle of regular expressions *shiver*.

      Comment

      • samatair
        New Member
        • Nov 2007
        • 61

        #4
        Using the basic's of PHP and some string functions, we can do this (I am not sure, this is the best efficent way) This would work fine with integers.
        If the number involves decimals don't use this script it would not work consistently (e.g 1.1 would work but 0.1 will not)
        If you understand the snippet, you will understand why?
        Code:
        <?php
        	$str = 'webandwe13';
        	$number = strrev(strrev($str) + 0); //extracting the number
        	$new_number = $number + 1; // increment the number by one
        	$str = str_replace($number, $new_number, $str); // replacing  with the old number
        	echo $str;
        ?>
        Output: webandwe14

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Markus
          Why not do yourself a favour and store the number in a database. Takes away the hassle of regular expressions *shiver*.
          That is the approach I would use. See this thread for a similar discussion.

          Comment

          Working...