PHP explode a string but keep the serialized part intact - php

I am trying to take a string, which is actually part of a insert statement, explode it ( it is separated by commas ) and then test reform the string depending on the index of the value. I have everything done but realize that exploding by a comma while potentially having a serialized value with the whole string as its own value will actually explode that serialized value which throws the whole thing off. Im trying to avoid having to go back and check before creating the string, that would be very involved but might be my only option. I was wondering if there is a way to take the initial string and separate it by the commas without separating the actual serialized value within the string. An example is below.
NULL,NULL,NULL,NULL,'hello world','a:3:{s:6:"johnny";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, johny";}s:5:"brady";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, brady";}s:5:"keith";a:3:{s:7:"physics";s:9:"great job";s:5:"maths";s:19:"you did a, good job";s:9:"chemistry";s:27:"need to work on this, keith";}}',NULL,23,14

Related

how to parse data already stored in database - PHP

I have a form users fill out that has one textarea. This gets stored in the database.
Now I want to sift through the data collected, get meaningful information out (like title) and store those in new fields in the database.
I know how to get the data out and store it in a variable. I'm not sure what to do next. I need to search the data for specific words like "title" and "instructions" and then focus in on everything after that word until a return was entered. Where should I start with this?
UPDATE
I really like the help so far, the expression match and preg_match_all / preg_match seem to get me started. One last question - I get how to find the start of the search, but how would I find the end? Usually the end will be denoted by a return (enter was pressed)
If there's any consistency to the text you can use a regular expression.
Otherwise you may want to update the form. Having to rely on consistent user input in a text field is iffy at best.
First of all, get data to a variable, then search in this variable to extract your data. The best way to do this is with Regular expression and with php functions preg_match and preg_match_all
EDIT:
For your second question you can search with regular expression to find \r and \n characters.
you can achieve this by using Explode() function.
Split the fields by using the separator. After converting the string to array, then you can parse the array again to get the Heading and value.
preg_match_all would be a good starting point.

Sorting user input

I attempting what I thought would be a simple exercise, but unless I’m missing a trick, it seems anything but simple.
Im attempting to clean up user input into a form before saving it. The particular problem I have is with hyphenated town names. For example, take Bourton-on-the-Water. Assume the user has Caps lock on or puts spaces next to the hyphens of any other screw up that might come to mind. How do I, within reason, turn it into what it’s meant to be?
You can use trim() to remove whitespace (or other characters) from the beginning and end of a string. You can also use explode() to break strings into parts by a specified character and then recreate your string as you like.
I think the only way you can really accomplish this is by improving the way the user inputs their data.
For example use a postcode lookup system that enters an address based on what they type.
Or have a autocomplete from a predefined list of towns (similar to how Facebook shows towns).
To consider every possible permutation of Bourton On The Water / Bourton-On-The-Water etc... is pretty much impossible.

Could you ever legitimately have an occurrence of "}{" in a JSON string

I am generating a sequential string of independent JSON strings for insertion into a single field in my database (the means justify the end), and am wondering if a JSON string could ever legally have an occurrence of two opposing(ly) faced curly braces such as }{? As I would like to use this pattern as a delimiter if so.
I am using PHP's json_encode function for this purpose.
Should say that I don't mean as a value - or key if that were possible as I am in control of the data. Seems like a stupid question now.
Yes. It can form part of a string in JSON text.
{
"EskimoKiss": "}{"
}
If you must store multiple pieces of data expressed as JSON in a database field, then parse them to objects, wrap them in an array, then serialise that array to JSON and store that.
You really should normalise the data though.

Zend Validate Int removes commas

I'm using the Zend_Validate_Int class to validate integers in a form. When the number is something like 1,000 it passes validation, but then removes the comma. Does anyone know a way I can get it to keep the comma?
Zend_Validate_Int is not your problem.
This validator accepts a value to validate and simply returns a boolean and only a boolean. It will validate as true an integer with a comma. The comma is str_replaced to empty but that value is never returned.
Your problem may be involved with any locale you have set or with any type casting you are using.
You can dump the variable values at every step to find where the actual change takes place.
Your best bet to fix this would likely be a view helper to format the values for output.
The purpose of Zend_Validate_Int is to make sure that final number you get is an integer not a float, you probably need to use another validator or a regular expression, or simply try Zend_Validate_Float.

Detect random strings

I am building a string to detect whether filename makes sense or if they are completely random with PHP. I'm using regular expressions.
A valid filename = sample-image-25.jpg
A random filename = 46347sdga467234626.jpg
I want to check if the filename makes sense or not, if not, I want to alert the user to fix the filename before continuing.
Any help?
I'm not really sure that's possible because I'm not sure it's possible to define "random" in a way the computer will understand sufficiently well.
"umiarkowany" looks random, but it's a perfectly valid word I pulled off the Polish Wikipedia page for South Korea.
My advice is to think more deeply about why this design detail is important, and look for a more feasible solution to the underlying problem.
You need way to much work on that. You should make an huge array of most-used-word (like a dictionary) and check if most of the work inside the file (maybe separated by - or _) are there and it will have huge bugs.
Basically you will need of
explode()
implode()
array_search() or in_array()
Take the string and look for a piece glue like "_" or "-" with preg_match(); if there are some, explode the string into an array and compare that array with the dictionary array.
Or, since almost every words has alternate vowel and consonants you could make an huge script that checks whatever most of the words inside the file name are considered "not-random" generated. But the problem will be the same: why do you need of that? Check for a more flexible solution.
Notice:
Consider that even a simple-and-friendly-file.png could be the result of a string generator.
Good luck with that.

Categories