substract string

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jan 24, 2013 3:12 pm

substract string

Postby expertalert01 » Thu Jan 24, 2013 3:19 pm

Hi I am an Very occasional programmer ,
I just need a small help
I want to cut the value before _ example

So basically, I need value like 600.1 , 2 , 931.5

Anything before _

which function shall i use ??
Can i get some example please

User avatar
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: substract string

Postby simplypixie » Thu Jan 24, 2013 11:50 pm

Like this (where $mystring is the name of the variables you have assigned your string(s) to)

Code: Select all

$mystring = substr($mystring, 0, strpos($mystring, "_"));

Just to explain:

strpos finds the position in the string of the first "_" so in your first example 600.1_GB_HPEG0600FBLSH that would be position 5.

substr then gets the part of the string based on the start position (0 - which is the very first character of the string) to the end position (5 - as returned by strpos).

So if we were to replace the code with the numbers it would look like this (as if you had put the numbers in manually)

Code: Select all

$mystring = substr($mystring, 0, 5);

Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests