views:

149

answers:

4

Hi!

I've got a huge problem. I made a special ID for the things in our webpage. Let's see an example:

H0059 - this is the special ID called registration number. The last two chars are the things' id.

I'd like to cut off the useless characters, to get the real ID, what means strip the first char, and all the 0s before any other numbers.

Example:

L0745 => 745, V1754 => 1754, L0003 => 3, B0141 => 141, P0040 => 40, V8000 => 8000

Please help me in this.

I've tried with strreplace and explode but failed :( Thanks for the help.

+3  A: 

ltrim(substr($input, 1), '0');

substr starts the string at the first character, skipping the letter.
ltrim will trim off all zeroes from the front.

Or, if you prefer: preg_replace('/^.0*/', '', 'L0003'); //returns 3

ryeguy
I don't think you code will work for L0003, P0040 and V8000 - the ltrim()
Phill Pafford
@Phill: Ltrim only trims off its character list until it hits a character that is not in its character list. It works with all examples you listed.
ryeguy
@ryeguy my bad, thought he was using an array, but if it's a string value then you're correct
Phill Pafford
A: 
$string = "L0745";
$string = intval(substr($string, 1));

That should take away the first letter and then intval will read it as a number so 007 will be 7.

Joe Majewski
intval will print a number starting with 0 as if it was in octal. Check the example for `intval(042);` on http://ar2.php.net/intval
Adriano Varoli Piazza
Didn't even think of that. Good call. :P
Joe Majewski
+3  A: 

You can use:

preg_replace("/^[^1-9]*(.*?)$/","$1",$str) as:

<?php

$arr = array('L0745','V1754', 'L0003', 'B0141', 'P0040', 'V8000');

foreach($arr as $str) {

    $str = preg_replace("/^[^1-9]*(.*?)$/","$1",$str);

    var_dump($str);
}

?>

Output:

C:\>php a.php
string(3) "745"
string(4) "1754"
string(1) "3"
string(3) "141"
string(2) "40"
string(4) "8000"

Explanation of the regex used: ^[^1-9]*(.*?)$

  • ^ - Anchor to start matching at the beg of the string.
  • $ - Anchor to match end of the string.
  • [1-9] - A single non-zero digit
  • [^1-9] - A single non 1-9 char...can include 0 or any other alphabet.
  • .*? - to match the rest
  • () - group and remember...and use in replacement.

This regex first by passes non 1-9 char at the beg of the string and matches and remembers the rest till the end ...and replaces the whole string with the remembered thing.

codaddict
simply `preg_replace("/^[^1-9]+/","",$str)`
stereofrog
Working answer *and* a thorough explanation. Nicely done.
Peter Bailey
You don't need to capture the remainder of the string. See my answer.
ryeguy
A: 

Looks to me like you want the keys and the index of the array to switch

try array_flip(array)

$arr = array(
   'L0745' => 745, 
   'V1754' => 1754, 
   'L0003' => 3, 
   'B0141' => 141, 
   'P0040' => 40, 
   'V8000' => 8000
);

$new_arr = array_flip($arr);

foreach($new_arr as $k=>$v) {
    echo "Key: ".$k." Value: ".$v."<br />";
}

echo "<pre>".print_r(array_flip($arr),true)."</pre>";
Phill Pafford
I don't think he has an array available, just that he showed how the ids were correlated with the strings he has.
Adriano Varoli Piazza
Good point @Adriano
Phill Pafford