substract string

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

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

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);

Post Reply