I'm trying to add double quotes to Every Variable like file: and label: in a json string. For example:
{file:"File_Name.mp3"},{file:"File_Name.mp4",label:"720"},{file:"File_Name.mp4",label:"360"}
Should be:
{"file":"File_Name.mp3"},{"file":"File_Name.mp4","label":"720"},{"file":"File_Name.mp4","label":"360"}
How can i do this? I read a article of stackoverflow There but my problem not solved with this. I'm supposed to use regular expressions. Unfortunately, but i am new.
and i need to get file name if label is 360 so what will be the php code give the filename as i point-out in php.
Thanks.
OK, if you're new to all this, maybe regular expressions aren't the best answer.
You can also use a simple str_replace function (doc) like this :
str_replace(array('file:', 'label:'),array('"file":', '"label":'),$your_json_string);
To exploit the json in php, you'll need to decode it using json_decode, and use a loop to check the label values (I advise a foreach loop).
Related
I'm using php to get the source html of a url. Once I have that source, I'd like to use regex to pull out a specific javascript variable value.
For Example:
<script>
let varOne.dataLayer['products'] = [
{"prdocutId":1,"productName":"foo"},
{"productId":2,"proudctName":"bar"}
];
// Here's a comment
let vartwo.dataLayer['foo'] = 'bar';
</script>
I've tried the following regex:
varOne.dataLayer\['products'\]\s?=\s?([^;]*)
This works, but only because there is no ";" in the products array anywhere. i.e. if the productName for productID 1 were to be something like "foo;but not bar" then the regex wouldn't work.
Is there a way to tell regex to pull the JSON object after "varOne.dataLayer['products'] = " so that I can confidently get the values of the array?
Here's a regex101 fiddle i've been playing with: https://regex101.com/r/EXgTW1/1
Regex would be tricky for this since you probably have no control over the variables or the JS formatting. If you can use a library like this one to turn the JS variables into PHP variables, it would be much less brittle.
I'm designing an API, but here is a problem:
?merchantCategoryName=[Adult,Fantasy,Mac]
&mappedCategoryID=[701,80,22]
&merchantID=36330&outputType=xml
This is ideal situation, but what if merchantCategoryName contains a comma like this: (Ad,ult is a merchantCategoryName)
?merchantCategoryName=[Ad,ult,Fantasy,Mac]
&mappedCategoryID=[701,80,22]
&merchantID=36330&outputType=xml
Then will be disorder, if I use base64 it seems not work, if I use htmlentities it seems ,'s entity is ,. so how can I solve this?
You can use this format:
?merchantCategoryName[]=Adult&
merchantCategoryName[]=Fantasy&
merchantCategoryName[]=Mac&
merchantID=36330&
outputType=xml
See this question for details
i need to remove () backslash in my string when using echo json_encode()?
my example..
$song_url = 116e9155e0afc11555cf33dc9c9bd25d.mp3
$resmsg[] = array("Song_name"=>"$song_name","Song_URL"=>"http://www.kbmusique.com/songs/$song_url");
echo json_encode($resmsg);
my output is
[{"Song_name":"djigh araouioui","Song_URL":"http:\/\/www.kbmusique.com\/songs\/116e9155e0afc11555cf33dc9c9bd25d.mp3"}]
but i need as
[{"Song_name":"djigh araouioui","Song_URL":"http://www.kbmusique.com/songs/116e9155e0afc11555cf33dc9c9bd25d.mp3"}]
Is there a way to solve this? Thank you.
Your comment indicates that you just need to get a copy/pastable URL for testing.
Just parse the JSON and extract the piece of data you need from it. i.e. If you want a text representation of something, then convert the JSON to text, don't try to hack the JSON into a specific form.
You could do this in PHP with json_decode, in a browser with JSON.parse(), or just use a tool such as the Chrome JSONView extension.
yeah, I know, the title is kind of confusing, but no better title came to my mind.
Here is my problem:
I want to use a link in my application, which would look like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
The problem is that &someparam2 is meant to hang on the second $_GET-Param.
It would be like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Instead, PHP interprets that &someparam2 hangs on the first $_GET-Param.
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Does anyone know a solution for this?
I already tried
localhost/index?jumpto='some_folder/somescript.php?someparam1=1234&someparam2=4321'
but of course that didn't work.
I hope you can understand my problem.
Thank you for your time.
You will need to URL encode your string some_folder/somescript.php?someparam1=1234 so that php will not parse & in the query string as a param separator.
use urlencode("some_folder/somescript.php?someparam1=1234");
I'm playing with the flickr api and php. I want to pass some information from PHP to Javascript through Ajax. I have the following code:
json_encode($pics);
which results in the following example JSON string:
[{"id":"4363603591","title":"blue, white and red...another seattle view","date_faved":"1266379499"},{"id":"4004908219","title":"\u201cI just told you my dreams and you made me see that I could walk into the sun and I could still be me and now I can't deny nothing lasts forever.\u201d","date_faved":"1259987670"}]
Javascript has problems with this, however, due to the unescaped single-quote in the second item ("can't deny").
I want to use the function json_encode with the options parameter to make it strip the quotes, but that's only available in PHP 5.3, and I'm running 5.2 (not my server). Is there a fast way to run through the entire array and escape everything before encoding it in Json? I looked for a way to do this, but it all seems to deal with encoding it as the data is generated, something I cannot do as I'm not the one generating the data.
If it helps, I'm currently using the following javascript after the ajax request:
var photos = eval('(' + resptxt + ')');
Have you considered using JSON2 instead of eval()? Details here.
str_replace('\'', '\\'', json_encode($pics))
You'll have to do a (recursive) foreach to walk through the array and manipulate them manually. You can do a str_replace, but addslashes works just as fine (and addcslashes is even better.)