0

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 ?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • Rachel, http://stackoverflow.com/questions/2210347/how-to-get-rid-of-extra-elements-in-string-literal-in-php has the answer that you accepted. There is no difference between the two, why ask another question? – Anthony Forloney Feb 08 '10 at 16:14
  • It does have difference, actually I wa about to refer that question in here – Rachel Feb 08 '10 at 16:18
  • That answer does not remove domain details from customer id – Rachel Feb 08 '10 at 16:19

6 Answers6

2

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
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 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 Feb 08 '10 at 16:08
  • @Rachel: `$theID` is your customer_id. – kennytm Feb 08 '10 at 16:12
2

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
  • 616,129
  • 168
  • 910
  • 942
1

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
  • 90,123
  • 14
  • 117
  • 115
1

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
  • 54,544
  • 15
  • 116
  • 120
  • @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 Feb 09 '10 at 14:32
1

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

Dolph
  • 49,714
  • 13
  • 63
  • 88
1
<?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
  • 5,460
  • 5
  • 34
  • 47