Modify each "segment" of string in PHP - php

For example I have string en/something1/something2 and I need to modify something in each segment.
Solution obviously is explode string with delimiter "/", modify each element and implode back. But I am fairly sure there is shorter way to do it.

Related

mysql value in array format, convert to PHP array

I have a part of my application that takes a bunch of values in JavaScript and stores them as an array in my MySQL database. Example of what this looks like in the database:
['123431234','3463412346','235456234','2352351','45623412']
When I grab this value in PHP, I can't seem to convert it to a PHP array properly. What's the proper method to converting a value like this that by default PHP considers a string into a PHP array?
You could try explode().
Split a string by string
You can explode your string as following:
$pieces = explode("','", $mysql_string)
You will then have to clean up the array a bit yourself. You can try str_replace to clean up any stuff you don't want.
Replace all occurrences of the search string with the replacement
string
For example:
str_replace('[', '', $pieces)
Or use an array to find everything you want to replace.
Good luck!
If you still can change the way the data is stored, you could think about json_encode() and json_decode() Which converts data into a well formatted string so it can be converted back easily. If you are communicating with a javascript client anyway, this could make your life much more comfortable.

preg_match removes escaping from json string

I am trying to parse some json data using json_decode function of php. However, I need to remove certain leading and trailing characters from this long string before decoding. Therefore, I am using preg_match to remove those characters prior to decode. For some reason, preg_match is changing escaping when it encounters following substring (in the middle of the string)
{content: \\\"\\200B\\\"}
After preg_match the above string looks like this:
{content: \\"\200B\\"}
Because of this, json_decode fails.
FYI, the preg_match pattern looks like this:
(?<=remove_these_leading_char)(.*)(?=remove_these_trailing_char)
OK, so here is the additional information based on the questions being asked:
Why triple escaping? fix triple escpaing etc. The answer is that I don't have any control over it. It is not generated by my code.
The original string is not fully json compliant. It has several leading and trailing characters that need to be removed. Therefore I have to use regex. The format of that string is like this:
returnedHTMLdata({json_object},xx);
It looks like this behavior is not limited to preg_match only. Even substr also does this.
It looks like you've got some JSON with padding. To remove the function name and parenthesis, leaving the (unescaped) json object, you can do something like this:
$str = <<<'EOS'
returnedHTMLdata({content: \\\"\\200B\\\", foo: \\\"bar\\\", \"baz\": \\\"fez\\\"},xx);
EOS;
$str = preg_replace('/.+?({.+}).+/','$1', $str);
echo $str;
Output:
{content: \\\"\\200B\\\", foo: \\\"bar\\\", \"baz\": \\\"fez\\\"}
Please note that even if you manage to successfully unescape this string, json_decode requires that keys - e.g. "content" - are enclosed in double quotes, so you will need to modify the JSON string/object before calling that function. Or I guess you could instead use something like the old Services_JSON package to decode it, which I believe does not have that requirement.

PHP - replace php formula

I am trying to work out the optimal way to replace all PHP variables within a string of code with a call to an array instead as shown below.
E.g. source code string
$random_var_name + $random_var_name2 * $diff_var_name3
Transformed into
$varArray["random_var_name"] + $varArray["random_var_name2"] * $varArray["diff_var_name3"]
I had thought that preg_replace() was the optimal solution, but the difficulty comes with the need to perform the replacement with a sub-part of the search pattern.
Perhaps it is better to just retrieve all the variables with a preg_match, edit/wrap them, then perform a single str_replace() for each variable?
However this is probably considerably slower.
The following regex should do what you're asking:
preg_replace('/\$([a-zA-Z_0-9]+)/', '$varArray["$1"]', $input_string);
In order to avoid to change $var['foo'] to $varArray["var"]['foo'] you have to check there're no [ character after the variable name. For this use a negative look-ahead:
$string = preg_replace('/\$(\w+)(?![\w\[])/', '$varArray["$1"]', $string);

handle parts of string in preg_replace_callback differently

I got a string in which I replace all occurrences of [CODE]...[/CODE]. With preg_replace_callback can I call a function which handles the content of those tags. But how can I manipulate all string which are around those occurrences?
Example:
$str = "Hello, I am a string with [CODE]some code[/CODE] in it";
Now, with preg_replace_callback I manipulate the content of [CODE], in this case some code. But I'd like for all other text in this string, so Hello, I am a string with and in it to do something different. How could I do this the best way?
Thank you for you help!
Flo
It'd be simpler if I could see the regex, but the gist is that I think you want capture groups.
You should be able to access those regions separately by placing them into parenthesis-wrapped groups. Each section will be available to your callback. So (crudely) something like /(.*)(\[CODE\].*\[/CODE\])(.*)/ should pass an array of matches to your callback

csv parsing, exploding avoiding ""

I have a csv file (really big) that I'm parsing with php.
Now is made like this.
x,y,z,value,etc
but sometimes there is this:
x,"blah,blah,blah",z,value,etc
doing this: explode(',',$string);
In case of a "" value also explode everything within.
array([0]=>x,[1]=>"blah,[2]=>blah,[3]=>blah"....)
What can I do to have this:
array([0]=>x,[1]=>"blah,blah,blah",[2]=>z....)
instead?
Thanks
Don't use explode, use fgetcsv.
For parsing just a string use str_getcsv if you have PHP >= 5.3.
If I were you and had to use the method you are saying. I would parse the string for text within the quotes via a regular expression.
Replace the , with a * (for example)
x,"blah*blah*blah",z,value,etc
then explode the string once again with the ,
Now you should get an approriate array but now you have the bla*bla*bla.
Then just do str_replace on the array
And in that way you should work..
This only applies if you have strict rules how to parse.. (in this case by explode);

Categories