Hello, HashCode :) Tell me, please, how can I get the value before a certain character? Example, there is a number:

0.35 

I need to get 0 and put it in a variable, or an example:

 234.23 

Enter 234 into a variable ... How can this be implemented? And everything after the point (including the point itself) would simply be ignored.

  • four
    (int) help ideone.com/p3tiBT - ReinRaus
  • The question was worth titling something like this: Getting the whole part from a number in php - nolka


3 answers 3

Work only with numbers? Or was it just for an example?

If you need to work the same way with strings (for example, AA.X - get AA), then you can use substr together with strpos .

This method can also be applied to numbers, but if work will be done only with numbers, then depending on the goals, it is better to use another. This may be a coercion to the whole, or first rounding in the right direction, and then coercion. About coercion of types it is better to read on. There just is almost your example.

    if only numbers, then int, and if any lines are delimited, then the first part can be taken like this:

     <?php $string="Hi:there"; $first=array_shift(explode(':',$string)); print $first; 

    http://codepad.org/P62p5iOi

    if the separator is always a dot and the first part obviously does not contain a directory separator (/ / mb in Windows \), you can do this:

     <?php $string="Hi.there"; $first=pathinfo($string,PATHINFO_FILENAME); print $first; 

    http://codepad.org/C9G5MUGX

    • According to the last: not working . Those. does not work as it should - returns everything except the last part, but as far as I understood, only the first part returns - BOPOH
    • I did not take into account the multiple division :) the latter case has a very narrow application - zb '26

    if only numbers, then:

     $int = 32.56; $result = floor($int); echo $result; 

    If the lines are also with explode, as described above ...