Replace string containing double qoutes and colon - php

I have this string saved in $metaV:
{"feedName":"Paypal Test","paypalEmail":"managercvtech#gmail.com","mode":"test","transactionType":"subscription","recurringAmount":"form_total","billingCycle_length":"1","billingCycle_unit":"day","recurringTimes":"0","recurringRetry":"0","trial_enabled":"1","trial_product":"73","trial_amount":"","trialPeriod_length":"1","trialPeriod_unit":"month","billingInformation_firstName":"4","billingInformation_lastName":"27","billingInformation_email":"5","billingInformation_address":"","billingInformation_address2":"","billingInformation_city":"","billingInformation_state":"","billingInformation_zip":"34","billingInformation_country":"","pageStyle":"","continueText":"","cancelUrl":"","disableShipping":"0","disableNote":"0","delayNotification":"0","selectedNotifications":"","feed_condition_conditional_logic":"0","feed_condition_conditional_logic_object":{"conditionalLogic":{"actionType":"show","logicType":"all","rules":[{"fieldId":"73","operator":"is","value":"1 month"}]}},"type":"subscription","recurring_amount_field":"form_total","update_user_action":"","delay_registration":"","update_site_action":""}
I want to replace this part of the string:
"trial_enabled":"0"
I tried to use str_replace() for this:
str_replace('\"trial_enabled\":\"1\"', '\"trial_enabled\":\"0\"',$metaV);

You could use json_decode and json_encode to transform your string into an array and the way back.
$data = json_decode($metaV, true);
$data['trial_enabled'] = "1";
$metaV = json_encode($data);

Related

PHP : Convert php dictionary data set to key:valu pair

I have data set smiller to dictonary and I want to get key value pair for this data.
here is DATA SET:
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
I am doing json_decode($data, true); but have no luck with it
Sorry If I am unclear.f
BTW I am doing it in PHP
the result should be like this:
test_field2: NONE
test_field3: NONE
Since your data is invalid json because of that u in it here is a solution
json_decode(str_replace("'",'"',str_replace("u'","'",$data)), true);
Should do the trick
You've to try like this because on your existing code couple of issue exists.
Replace Unnecessary character u' with '
Replace single quotes(') on json string. For json double quotes(") is permitted.
So just search these two characters and replace with '' and " respectively.
<?php
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}";
$search = ["u'","'"];
$replace = ["'",'"'];
$without_u = str_replace($search,$replace,$data);
$array = json_decode($without_u, true);
print '<pre>';
print_r($array);
print '</pre>';
?>
DEMO: https://eval.in/1032316

Extracting data from Json in Php

I have this Json Object Below, I want to extract this data and output it in PHP
{"seat_booked":"A5","0":"A5","1":"A3"}
then get them into this format
$seat_booked = "'A5', 'A5', 'A3'";
How can I do this?
I hope you are looking for this, its very simple example by using json_decode():
$string = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = json_decode($string,true);
$resuiredString = '"'."'".implode("','", $decoded)."'".'"';
echo $resuiredString;
Result:
"'A5','A5','A3'"
Side Note:
I suggest you to learn about variable concatenation.
PHP Concatenation
Another solution:
$json = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = array_map(
function($val) {
return "'". $val."'";
},
array_values(json_decode($json, true))
);
To get an object from a json in php you can use json_decode has explained here.
But you have another problem, your json is wrong!
If you want to represent a single dimensional array you should at least do this
["A5","A5","A3"]
Finally, using json_decode:
$obj = json_decode('["A5","A5","A3"]');
var_dump($obj);
Also, you could do something like:
{"0":"A5","1":"A5","2":"A3"}
$obj = json_decode('{"0":"A5","1":"A3", "2": "A5"}', true);
var_dump($obj);
Edit:
It's not very clear from your question if you are trying to get back an object from a json or if you just want to get a string from it.
If what you need is an string then you don't even need json, you could do this by string manipulation and/or using regex.
But just for completeness, if a quoted comma separated string is what you need you can do this:
$array = json_decode('["A5","A5","A3"]');
$str = implode("','",$array);
$str = "'" . $str . "'";
var_dump($str);

How to convert string variable to an array in php?

I have one string variable as
$p_list="1,2,3,4";
i want to convert it to an array such as
$a[0]='1';
$a[1]='2';
$a[2]='3';
$a[3]='4';
how to do this in php?
Use explode.
$p_list = "1,2,3,4";
$array = explode(',', $p_list);
See Codepad.
Try explode $a=explode(",","1,2,3,4");
Try this:
In PHP, explode function will convert string to array, If string has certain pattern.
<?php
$p_list = "1,2,3,4";
$noArr = explode(",", $p_list);
var_dump($noArr);
?>
You will get array with values stores in it.
Thanks

preg_replace a part of a string with variable

I have a string that looks a little something like this:
$string = 'var1=foo&var2=bar&var3=blahblahblah&etc=etc';
And I would like to make another string from that, and replace it with a value, so for example it will look like this
$newstring = 'var1=foo&var2=bar&var3=$myVariable&etc=etc';
so var3 in the new string will be the value of $myVariable.
How can this be achieved?
No need for regex; built in URL functions should work more reliably.
// Parse the url first
$url = parse_url($string);
// Parse your query string into an array
parse_str($url['query'], $qs);
// Replace the var3 key with your variable
$qs['var3'] = $myVariable;
// Convert the array back into a query string
$url['query'] = http_build_query($qs);
// Convert the $url array back into a URL
$newstring = http_build_url($url);
Check out http_build_query and parse_str. This will append a var3 variable even if there isn't one, it will URL encode $myVariable, and its more readable than using preg_replace.
You can use the method preg_replace for that. Here comes an example
<?php
$string = 'var1=foo&var2=bar&var3=blahblahblah&etc=etc';
$var = 'foo';
echo preg_replace('/var3=(.*)&/', 'var3=' . $var . '&', $string);

Convert string (similar json format) to object or array

I receive string "{success: false, errors: { reason: 'text text text' }}" by CURL, how to convert this string to array or object?
String '{"success": "false"....}' may be converted to object by json_decode, but I have string without qoutes.
Use this regex first (it adds quotes)
$json = preg_replace ('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/u', '"$1"', $string);
After that, you can simply use json_decode()
$array = json_decode ($json);
Update
I found this script somewhere:
function json_fix_quotes ($string){
$string = str_replace("{",'{"',$string);
$string = str_replace(":'",'":"',$string);
$string = str_replace("',",'","',$string);
$string = str_replace("'}",'"}',$string);
return $string;
}
Try that instead of the regex

Categories