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.
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 an answer here:
How to replace multiple items from a text string in PHP? [duplicate]
(1 answer)
Closed 6 years ago.
I have the following string:
$string = java-developer-jobs
I would like to remove - and jobs from $string, but I don't want to use str_replace multiple times.
Use array as :
$toReplace = array('-','jobs');
$with = array('','');
$string = 'java-developer-jobs';
str_replace($toReplace,$with,$string);
You can use regex for this.Try something like this:
$string = "java-developer-jobs";
$sentence = preg_replace('/\-|jobs/', ' ', $string);
echo $sentence;
Output: java developer
LIVE DEMO
This question already has answers here:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);
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')
This question already has answers here:
Replace last occurrence of a string in a string
(15 answers)
Closed 9 years ago.
I need to remove the domain name from the end of a string. I have tried the following code:
$domainNAME="example.com";
$result="_xmpp-client._tcp.example.com..example.com"
$string = $result;
$string = str_replace(".".$domainNAME, " ", $string);
Here the result is "_xmpp-client._tcp.". But the result should be "_xmpp-client._tcp.example.com.".
I need to remove the domain name only from the end of string, if domain name exists anywhere in the string it should not be removed.How can I change the code for that?
Any help should be appreciated!
No need for fancy preg nor substr.. just use trim() function :)
will remove from both ends
echo trim($string,$domainNAME);
will remove domainName from end of string
echo rtrim($string,$domainNAME);
will remove domainName from beging of string
echo ltrim($string,$domainNAME);
Example
echo rtrim('demo.test.example.com',"example.com");
//#return demo.test
2nd method
if not.. then use the preg match :).
$new_str = preg_replace("/{$domainNAME}$/", '', $str);
this will replace $domainNAME from $str ONLY if its at end of $str ($ sign after the var means AT end of string.
You could use preg_replace and specify the end of string marker $:
$string = preg_replace("/" . $domainNAME . "$/", " ", $string);
$domainNAME="example.com";
$result="_xmpp-client._tcp.example.com..example.com";
$string = $result;
$string = substr($result,0, strrpos($result, $domainNAME)-1);
echo $string;
if you are truly wanting to have the output as _xmpp-client._tcp.example.com. with the dot at the end use
preg_replace("/\." . $domainNAME . "$/", " ", $string);
and you can add ? if you want it to be optional
preg_replace("/\.?" . $domainNAME . "$/", " ", $string);
Demo
$domainNAME="example.com";
$length = strlen(".".$domainNAME);
$result="_xmpp-client._tcp.example.com..example.com";
$string = substr($result, 0, $length*(-1));
Try that. I wish it could help you
Use str_ireplace('example.com.example.com', 'example.com', $string);