views:

77

answers:

6

Customer id literal has customer_id+Domain_details, eg.: 998787+nl and now I just want to have 998787 and not +nl, how can this be acheived this in php

Question:

I have number like 9843324+nl and now I want to get rid of all elements including + and afterwards at the end and only have 9843324 and so how should I do this in php ?

Right now I am having $o_household->getInternalId returns me 9843324+nl but I want 9843324, how can I achieve this ?

Thanks.

Thanks.

Update :

    list($customer_id) = explode('+',$o_household->getInternalId());

Will this solve my problem ?

+2  A: 

If you don't want to keep the leading zeros, simply convert it into an integer.

$theID = (int)"9843324+nl";
// $theID should now be 9843324.

If the + is just a separator and the sutff before can be a non-number, use

$val = "9843324+nl";
$theID = substr($val, 0, strcspn($val, '+'));
// $theID should now be "9843324".
KennyTM
I do not have leading zeros here, only customer_id+domain_name but I want to remote `+domain_name` and only have customer_id, hope I am making myself clear here.
Rachel
@Rachel: `$theID` is your customer_id.
KennyTM
+1  A: 

If you need it to remain a string value, you can use substr to cut the string down to its starting index to the 3rd from last character, omitting the domain details +nl

$customer_id = substr($o_household->getInternalId, 0, -3);
Anthony Forloney
+2  A: 

Easy way? Just cast it to an int and it will drop off the extra stuff.

<?php
$s = '998787+nl';
echo (int)$s;
?>

Output:

998787
cletus
+1  A: 

As a slightly more general solution, this regular expression will remove everything that isn't a digit from the string $str and put the new string (set of numbers, so it can be treated as an integer) into $num

$num = preg_replace('/[^\d]/', '', $str);
Yacoby
@Downvoter, Please comment as to why. This works exactly as the questioner wanted and provides a good alternative to all the other methods suggested.
Yacoby
A: 

Check out the explode() function, and use + as your delimiter.

Dolph
+1  A: 
<?php
$plusSignLoc = strpos($o_household->getInternalId, "+");
$myID = substr($o_household->getInternalId, 0, $plusSignLoc);

//Debug (Verification)
echo $myID;
?>

This will find the + sign, and insure that anything and everything after it will be removed.

Urda