This question already has answers here:
How to strip trailing zeros in PHP
(15 answers)
Closed 7 years ago.
I have a string like this:
14522354265300000000000
I want to display it without zero values, how I can do this? I do this
$pos = strpos($route, '0');
$length = count(str_split($route));
$a = $length - $pos;
$a = substr($route, 0, $a);
but it remove 3 in the end of string. Can somebody help me?
Additional:
If string will be 123088888880, I want make it 123.
You can use rtrim for this:
echo rtrim("14522354265300000000000", "0"); // outputs: 145223542653
here's a nice algo:
<?php
$string = "14522354265300000000000";
$new_string = '';
for($i=0; $i<strlen($string) ; $i++){
if($string[$i] != '0'){
$new_string .= $string[$i];
}
}
echo $new_string;
?>
rtrim is only if you have zero's at end of string :)
You can use rtrim('14522354265300000000000', '0')
Related
This question already has answers here:
Replace character's position in a string
(4 answers)
Closed 3 years ago.
The code is in below.I have a string.i want to replace a specific character with its fixed position.
$string = 'syeds nomasn shibsly';
$char = 't';
$position = [0,4,10];
foreach($position as $pos) {
$str = substr_replace($string, $char, $pos);
}
echo $str;
Output will be tyedt nomatn shibsly
Hope you got my problem. Please help me.
You can access the individual characters of strings like an array:
foreach ($position as $pos) {
$string[$pos] = $char;
}
Your original code works fine with a couple of modifications.
You're not updating $string inside the loop, so your $str variable will end up with only the final position changed, since each iteration looks at the original string. You also need to pass in the length parameter to substr_replace, so that the correct portion of the string is replaced.
Try changing the code to:
foreach ($position as $pos) {
$string = substr_replace($string, $char, $pos, 1);
}
echo $string;
tyedt nomatn shibsly
See https://eval.in/968698
(You also need to fix the array syntax to use square brackets)
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I need someone who can make the following output which is a single string
[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1
to
{"mobile":"XXX-XXX-XXXX",
"permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}",
"tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
Help me removing first '[' and last ']1' in the above string. Thanks in advance
This should work for you.
$final_str = rtrim(ltrim($your_str, '['), ']1');
Try below code,
<?php
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$temp_str = preg_replace('/\[/',"",$str);
$new_str = str_replace("]1","",$temp_str);
echo $new_str;
?>
Output,
{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
This will remove first and last character from your string
$result = substr($string, 1, -2);
Here is the link if you want to explore more:
https://www.w3schools.com/php/func_string_substr.asp
Try ltrim() and rtrim()
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$getTrim = ltrim($str, '[');
$getTrim = rtrim($getTrim, ']1');
echo $getTrim;
OR
$getTrim = rtrim(ltrim($str, '['), ']1');
you can use str_replace or preg_replace replace your keyword with empty string.
This question already has answers here:
How to remove part of a string after last comma in PHP
(3 answers)
Closed 3 years ago.
I would like to remove the last hyphen and anything after it in a string. After looking I found something that does the first hyphen but not last:
$str = 'sdfsdf-sdfsdf-abcde';
$str = array_shift(explode('-', $str));
Current String
$str = 'sdfsdf-sdfsdf-abcde';
Desired Result
$str = 'sdfsdf-sdfsdf';
You can use this preg_replace:
$repl = preg_replace('/-[^-]*$/', '', $str);
//=> sdfsdf-sdfsdf
-[^-]*$ will match - followed by 0 or more non-hyphen characters before end of line.
You can use strrpos to get the last index, and then use substr to get the desired result.
$str = 'sdfsdf-sdfsdf-abcde';
$pos = strrpos($str , "-");
if ($pos !== false) {
echo substr($str, 0, $pos);
}
You're close. Just use array_pop() instead of array_shift(). array_pop() removes last element of array. You need, of course, use implode() later to put the strign together again.
$arr = explode('-', $str);
array_pop($arr);
$str = implode('-', $arr);
It's important not to do that in one line since array_pop() works on a reference to the array and it modfies it, and then returns only removed element.
There are a few other possible solutions mentions by other answers.
This is a little bulky but it will work for you:
$str = 'sdfsdf-sdfsdf-abcde';
$pieces = explode("-",$str);
$count = count($pieces);
for ($x = 0; $x <= $count - 2; $x++) {
$desired_result .= $pieces[$x].'-';
}
$desired_result = substr($desired_result, 0, -1);
echo $desired_result;
if you have a lot of them you can use this function:
function removeLast($str){
$pieces = explode("-",$str);
$count = count($pieces);
for ($x = 0; $x <= $count - 2; $x++) {
$desired_result .= $pieces[$x].'-';
}
$desired_result = substr($desired_result, 0, -1);
return $desired_result;
}
you call it by:
$str = 'sdfsdf-sdfsdf-abcde';
$my_result = removeLast($str);
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 7 years ago.
I'm wondering how to extract numbers from a string for example
$string = "1.8 to 250"
What i want to get is,
$a = 1.8
$b = 250
Thanks for any help provided
Try this :
$str = '1.8 to 250';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
From this post Extract numbers from a string
Try this:
$str = '1.8 to 250';
$string = explode(" ",$str);
//print_r($string);
for($i = 0;$i < count($string);$i++){
if(is_numeric($string[$i])){
print "\n $string[$i]";
}
}
This question already has answers here:
Get first 100 characters from string, respecting full words
(18 answers)
Closed 8 years ago.
How to make string shorter by cut it on the last word?
like example, allowed symbols are 10, and echo only these words which fits in this limit.
$string = 'Hello Hello John Doe'
// Limit 10. Expected result:
$string = 'Hello'
// Limit 12. Expected result:
$string = 'Hello Hello'
...
All I can find in manual is cutting string by symbols, not by words. There are some custom functions to do so, but maybe there are php command for this?
This should work:
$str = "i have google too";
$strarr = explode(" ", $str);
$res = "";
foreach($strarr as $k)
{
if (strlen($res.$k)<10)
{
$res .= $k." ";
}
else
{
break;
};
}
echo $res;
http://codepad.org/NP9t4IRi
Tried to edit Mike's answer, to fix the last word thing, but was not able to.
So here is his solution with the fix:
$str = "Hello Hello My name is Hal";
$len = 10;
if ( strlen( $str ) > $len )
{
$out = substr($str,0,$len);
if ( $str[$len] != ' ')
{
$out = substr($out,0,strrpos($out,' '));
}
}
echo $out; // Hello
Edit: update version to cope with word breaks better.
This shouldn't be too difficult. Truncate to the maximum length, then truncate to the last space. Add an adjustment for lengths that fall on the end of words
<?php
$str = "Hello Hello My name is Hal";
for ($i = 3; $i <30;$i++) {
echo "'".trunc($str,$i)."'\n";
}
function trunc($str, $len) {
$str.=' ';
$out = substr($str,0,$len+1);
$out = substr($out,0,strrpos($out,' '));
return trim($out);
}
Here's a codepad version