How Do I remove last comma from string in php [duplicate] - php

This question already has answers here:
Json_encode or remove last comma?
(3 answers)
How do I remove a comma off the end of a string? [duplicate]
(10 answers)
Closed 1 year ago.
I want to remove the last comma from below array.
{"location":"Park Town,Chennai,Tamil Nadu" },
{"location":"Arajar Salai,Simmakkal,Madurai" },

Jon Stirling have right in his comment. If you try to create json manually - it's bad idea.
But anwser to your question should be something like this (didn't test it) :
<?php
$tab=explode(",",$string);
array_pop($tab);
$string=implode(",",$tab).']';
?>

Just use php rtrim() function on the string you will get your result.
like below
<?php
$text = '{"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" },';
$test = rtrim($text,',');
echo $test;
?>
A live example https://eval.in/849038

Related

How to split this type of string and get particular values in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I Have a string like this
{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}-
I need the value of Balance.
I tried like this.
$string = '{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":1,"Code":1,"Message":"Success"}-';
$withCharacter = strstr($string, 'Balance":');
echo substr($withCharacter, 1);
Tried to use explode also but no luck.
This seems like a valid JSON, why not json_decode and find the value:
$i = json_decode('{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}');
echo $i->Account->Balance;

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

How to remove specific part from a String in php [duplicate]

This question already has answers here:
How can I remove part of a string in PHP? [closed]
(9 answers)
Closed 8 years ago.
I have the following String
Hi this string is to #removeStart be modified by #removeEnd function
And I want to remove the string part coincides by #removeStart and #removeEnd and these two tags from the above string
Try this
$str = 'Hi this string is to #removeStart be modified by #removeEnd function';
echo preg_replace('/#removeStart (.*) #removeEnd/','',$str);
Check this Demo Code Viper
Pattern Check
(#\b\w+)
PHP
<?php
$str="Hi this string is to #removeStart be modified by #removeEnd function";
echo preg_replace('(#\b\w+)','',$str);
?>
Result
Hi this string is to be modified by function

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories