Joe
Joe

Reputation: 33

How do I remove or cut some number in php

Let's say I have this number 0601811002 and I want cut the number.

Example :-

I want remove 5 chars

0601811002and will be 06018

If I want remove 4 chars

0601811002and will be 060181

If I want remove 7 chars

0601811002and will be 060

Let me know

Upvotes: 0

Views: 709

Answers (5)

hmldd
hmldd

Reputation: 11

If it's real number(not quoted by ' or "), you can cut it in math way.

<?php
    $str = 601811002;
    $str_1 = floor($str/100000);  //cut last 5 numbers
    echo $str_1;                  //6081

    $str_2 = floor($str%100000);  //retains last 5 numbers
    echo $str_2;                  //11002
?>

Upvotes: 0

Joe Landsman
Joe Landsman

Reputation: 2177

<?php
$str = '0601811002';
$str = substr($str, 0, -4); // Chop 4 characters off of string
echo $str;                          // 060181
?>

Upvotes: 0

genesis
genesis

Reputation: 50974

echo substr("0601811002", 0, -5);

will cut off last five characters

http://sandbox.phpcode.eu/g/822fc.php

result: 06018

http://php.net/manual/en/function.substr.php

Upvotes: 3

Grigor
Grigor

Reputation: 4059

$string = substr("0601811002", 0, -4);

cuts 4 characters

Upvotes: 1

pkk
pkk

Reputation: 3701

I guess all you need is substr:

is http://php.net/manual/en/function.substr.php

More precisely (based on your examples):

<?php
    $cut_5_chars = substr("0601811002", 0, -5);
    $cut_4_chars = substr("0601811002", 0, -4);
    $cut_7_chars = substr("0601811002", 0, -7);
?>

Try it out

Upvotes: 0

Related Questions