0

using PHP, How can i remove the rest of character if sting_Len is greater than 6 for example i have 600275L and i want to end up like 600275 but only if greater than 6 digit. i used the following code to to extract value starts with 600, i want update it to work for the above condition, Thank you

    if((substr($key, 0, 3) == "600") && ($row['ItemClass']==3)) 
    {
     $assy = $key;
     $rout = "Assy";
    }
NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
Kaka
  • 37
  • 7

2 Answers2

1

If you always want to limit it to six characters, then you should just be able to use substr for this without checking the length. If you write:

$string = 'abcdefg';
$string = substr($string, 0, 6);

then $string will equal 'abcdef'.

But if $string is shorter than 6 characters, it will just return the entire string. So if you write:

$string = 'abc';
$string = substr($string, 0, 6);

then $string will equal 'abc'.

You can see this in the PHP manual here.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

Use the following logic

if(strlen($code) > 6) { echo substr($code, 0, 6); }
Carca
  • 564
  • 1
  • 6
  • 16