How to replace string from getting postman as parameter - php

Currently i am facing issue of replace string , I used postman & passing string in postman like [{"id":"115","flag":"1","qty":"3","size":"10"}] as a parameters but when i print string i am getting output like [{\"id\":\"115\",\"flag\":\"1\",\"qty\":\"3\",\"size\":\"10\"}] , So i want to only remove '\' from string i have tried following code but not work.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo $res = preg_replace("/[^a-zA-Z]/", "", $fliesid_in_store);

Have you tried stripslashes.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo stripslashes($fliesid_in_store);

The string you mentioned is in json format
$json = [{"id":"115","flag":"1","qty":"3","size":"10"}] //this is json
Assign that to variable and decode it.
$string = json_decode($json,TRUE) this give result in array format
In your case
$string = json_decode($_REQUEST['fliesid_in_store'],TRUE);

Related

PHP - substr() bug or invalid argument

I working at PHP Project With PHP Version 7.0.13
I was dealing with JSON lately, I have a JSON file that needs to be decode to PHP but before I decode the JSON, I need to clean some abstract string inside the file that JSON obtained inside, to clean the string using substr() to get the JSON.
when i write the code, like this:
$jsonraw = "\"{ JSON should be here, later }\"";
$cutstart = strpos($jsonraw, "{");
$cutend = strrpos($jsonraw, "\"");
$jsonclean = substr($jsonraw, $cutstart, $cutend);
echo $jsonclean;
The output is like this
{ JSON should be here, later }
But when the string is like this
$jsonraw = "\"some abstract string to remove { JSON should be here, later }\"";
The output is became like this
{ JSON should be here, later }"
As we can see there was a quote symbol " at the last of the string, I was trying to decrement the $cutend, like this $jsonclean = substr($jsonraw, $cutstart, --$cutend); and this to $cutend-1
Any help, I appreciate.
Sorry for my bad English
You can use preg_match to get the json from that string:
$string = "some abstract string to remove { JSON should be here, later }";
preg_match('/\{.*\}/', $string, $match);
var_dump($match[0]);
the result would be:
string(30) "{ JSON should be here, later }"
As the third parameter is the length of the string, you need to say that the length is the end position minus the start position...
$jsonclean = substr($jsonraw, $cutstart, $cutend-$cutstart);

Array turn to json using php

need your help on this one...
I'm trying to create a code that will get a .txt file and convert all text content to json.
here's my sample code:
<?php
// make your required checks
$fp = 'SampleMessage01.txt';
// get the contents of file in array
$conents_arr = file($fp, FILE_IGNORE_NEW_LINES);
foreach($conents_arr as $key=>$value)
{
$conents_arr[$key] = rtrim($value, "\r");
}
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
echo $json_contents;
?>
I already got the result when i tried to echo the $json_contents
["Sample Material 1","tRAINING|ENDING","01/25/2018 9:37:00 AM","639176882315,639176882859","Y,Y","~"]
but when I tried to echo using like this method $json_contents[0]
I only got per character result.
Code
Result
hope you can help me on this one..
thank you
As PHP.net says
"Returns a string containing the JSON representation of the supplied value."
As you are using $json_contents[0] this will return the first char of the json string.
You can do this
$conents_arr[0]
Or convert your json string to PHP array using
$json_array = json_decode($json_contents, true);
echo $json_array[0];
It is happening because $json_contents is a string. It might be json string but it's string so string properties will apply here and hence when you echo $json_contents[0] it gives you first character of the string. You can either decode the encoded json string to object like below:
$json = json_decode($json_contents);
echo $json[0];
or echo it before the json_encode:
echo $conents_arr[0];
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
json_encode() function takes an array as input and convert it to json string.
echo $json_contents; just print out the string.
if you want to access it you have to decode the JSON string to array.
//this convert array to json string
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
//this convert json string to an array.
$json_contents = json_decode($json_contents, true);
//now you can access it
echo $json_contents[0];

Replace tab & new line feed with empty string in JSON string

I have a JSON string which is like this when I log it:
..."items":{"0":"
\t\t\t\t\t"},"buys":{"0":"
\t\t\t\t\t"}}}, "sells":{}, "clients":{"test":{"0":"
\t\t\t\t"}...
What I want is to delete all new line feed (\n) & tab characters(\t) from it and have a JSON string like this :
..."items":{},"buys":{}}}, "sells":{}, "clients":{"test":{}...
I have written a preg_replace function like this :
$json = preg_replace(['/\"0\"\:\n\"[\\t]+/'], [''], $json);
but When I log the JSON string they have not been deleted .
What should I do ?
You can use str_replace() function to replace the \t and \n:
$json = str_replace(array('\t','\n'),'',$json);
You can read more about this here.
You may use this regular expression "[\t\n\r]"

After Explode() geting result: %C4%84

My problem is that after exploding a string I get this: %C4%84 value
but I should get Ą. I can convert it using preg_replace, but is there simple way to convert ?
$band_song = explode('/',$url,6);
echo $band5=$band_song[3].' '.$band_song[4]);
You can simply urlencode your variables.
$var = urlencode($original_var);
EDIT
You'd want decode, not encode sorry
$var = urldecode($original_var);

Regex not displaying well whe using PHP preg_quote

Hi Im trying to concatinate a string before converting that string into regex in PHP, but the problem is it's not displaying as expected. I've been searching using google and found out about preg_quote, problem is it's not working well.
Here is my example:
$mystring = "banana"; // put this to a variable assume this value is dynamic
$regex_str = "/^"$mystring"\-[a-z0-9]\-[a-z0-9]$/";
//Im expecting expecting /^banana\-[a-z0-9]\-[a-z0-9]$/
$regex = preg_quote($regex_str);
but what I am getting is:
/\^banana\\\-\[a\-z0\-9\]\\\-\[a\-z0\-9\]\$/
and always returning the wrong value.
Call preg_quote() on the string you're adding before you add it into the regex:
$mystring = "banana";
$regex_str = "/^" . preg_quote($mystring, "/") . "\-[a-z0-9]\-[a-z0-9]$/";

Categories