Removing extra spaces from within quotes of a string in PHP

Every once in a while, you may be building strings that include quoted material. The issue is that if you’re doing it programmatically, you may be doing something that quotes different parts of the string. The issue is, that if the field you’re pulling from doesn’t exist, you may leave yourself with extra spaces in your strings. See the example below. Table: A Columns: color,number,price,shape Row1: red,4,3.25,square Row2:...

Read More

Parse a CSV file line in PHP

Parse a CSV file line in PHP

PHP has great string manipulation tools for importing files like fgets() to get a single line or file() to dump the lines into an array right at import. Explode is a great function when you start dealing with the contents of the file. However, if you’re bringing in a standardized file, like a CSV, there are some issues with explode. For instance, a comma may be in a string in the middle of the field, so you can’t break it apart on...

Read More

Split a string apart into an array in VB .Net

I like using the Split function to break apart strings, but you’re forced to have a delimiter then. Passing it an empty string won’t return each character. If you want to convert a string into an array of characters so you can loop over each one, the solution is quite simple. Use the following code. Dim charArr() As Char = myString.ToCharArray

Read More

PHP Levenshtein distance

Levenshtein distance is a way of calculating how different two strings are. This function is extremely helpful in determining if a user made a mistake in entry, trying to find similar inputs, or comparing strings for any reason. The Levenshtein distance is a numeric value of how many changes are necessary to turn the one string into the other. There are default weights assigned to different operations like Capitalizing a letter or...

Read More

MySQL Replace function

I don’t recommend over using the REPLACE() function in MySQL.  It completely destroys performance.  However, sometimes, either by poor design, or just oversight, you have to replace something in order to make a join work, or the application code work.  REPLACE() is case-sensitive. The syntax is pretty simple: REPLACE(`field`, ‘find’, ‘replacewith’) Example: Fruit Values: Apple, Orange, Pear,...

Read More