here is field name "phone number". i want to store data in the database table without leading zero when a user can input phone number like '01323442234' or '1323442234'.
input = '01323442234' or '1323442234'
store = '1323442234' (skip first zero)
$trimmed_phone = ltrim($input, "0");
ltrim will trim all the leading character from a string, no other characters will be removed.
doc: https://www.php.net/manual/en/function.ltrim.php
There are a lot of way to remove 0 from the first part of a string.
If it contains only number, then cast it to integer
$var = (int)$var;
You can use left trim as follow :
$var = ltrim($var, '0');
Just use + inside variables:
echo +$var;
Multiple it by 1 :
$var = "0000000000010";
print $var*1; // prints 10
Note : If your string contains without number, then only use ltrim
Related
I have a string /en/products/saucony-switchback-iso/416.html and I would like to replace the first 4th character /en/ with /de/.
The result should be /de/products/saucony-switchback-iso/416.html
This is what I've tried:
$href = "/en/products/saucony-switchback-iso/416.html";
$href_replace = substr_replace($href, "/de/", 0);
its only returning "/de/"?
You also need to define the length of how much you're replacing in the string, which in your case is 4 (or 3, seeing as the trailing / is present in both) characters.
$href = "/en/products/saucony-switchback-iso/416.html";
$href_replace = substr_replace($href, "/de/", 0, 4);
echo $href_replace;
If you don't define a length as in your example, it defaults to the entire length of the string http://php.net/manual/en/function.substr-replace.php
length
If given and is positive, it represents the length of the portion of
string which is to be replaced. If it is negative, it represents the
number of characters from the end of string at which to stop
replacing. If it is not given, then it will default to strlen( string
); i.e. end the replacing at the end of string
Which is why you're only being left with /de/
If you want to replace /en/, str_replace is a better function
echo str_replace("/en/", "/de/", $href);
Technically you only need to do 3 (but whatever), it's simple.
echo "/de" .substr("/en/products/saucony-switchback-iso/416.html", 3);
Output
/de/products/saucony-switchback-iso/416.html
Sandbox
I also think substr will be about as fast as you can get it.
I have a string like any of the following:
$str = 'A001';
$str = 'B001';
$str = 'AB001';
$str = 'B0015';
....
$str = '001A';
I want to keep only 3 characters from the end of each string.
My code is like this:
$code = str_split($str);
$code = $code[1].$code[2].$code[3];
But it works for specific cases, but not for general ones! How I can get it for general ones?
I want to keep every 3 character from end of string
Simply Use substr
echo substr($str,-3); // Last 3 characters
Second parameter to this function is start, and according to the Manual
If start is negative, the returned string will start at the start'th character from the end of string.
Fiddle
Use sbstr()
echo substr($str,-3);//get last 3 char char
Or try:
echo $str[strlen($str)-3].$str[strlen($str)-2].$str[strlen($str)-1];
You need to use substr function.
All you need to do is to pass the string, and tell it where you cut the string off. If you want to cut the string off from end, you have to provide the value in negative.
substr($str, -3);
// The third argument is optional, which specifies the length of the returned string.
$databtcguild = file_get_contents('http://btcguild.com');
preg_match('~<b>Pool Speed</b></a> (.*?) TH/s~',$databtcguild,$btcguild);
$btcguildhashrategh = ($btcguild[1] * 1000);
echo $btcguildhashrategh;
echo "<br>";
echo $btcguild[1];
For some reason this code is outputting the wrong answer. For example, $btcguild[1] will equal 12,747 and this code will output 12000. I'm completely lost here. Thanks for any help.
The "hash speed" value you are extracting from that site has a value with a comma in it:
12,747
PHP needs to convert this string to a numeric value, and the comma causes the numeric value 12 to be returned (, is interpreted as a decimal)
Make sure you strip all non-numeric characters before multiplying:
//keep only values 0-9 and decimal (period)
$hash_speed = preg_replace("/[^0-9.]/", "", $btcguild[1]);
$btcguildhashrategh = ($hash_speed * 1000); //returns 12747000
Try explicitly type-casting the result: $btcguildhashrategh = ((double)$btcguild[1] * 1000);
Otherwise PHP will convert it into an int.
I need to remove the third character of a string. Here's what I tried.
$my_string['2'] = "";
This worked find, but when I tried to put this variable in a MySQL query, it returned errors.
What is the best way to remove a certain character (character 3 in my case) from a string?
Try out substr_replace.
For example:
$new_string = substr_replace($my_string, '', 2, 1);
Use an integer as index, not a string:
$my_string[2] = '';
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 11 months ago.
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 ?
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".
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
<?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.
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);
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);
Check out the explode() function, and use + as your delimiter.