Want the best way to trim a sting in javascript?
A while ago, I came across a post on such a subject. It was written a few years ago but it’s still as relevant and useful as ever.
Want the best way to trim a sting in javascript?
A while ago, I came across a post on such a subject. It was written a few years ago but it’s still as relevant and useful as ever.
I have written a simple tool to tidy assignments. It basically takes a group of assignments and uses spaces to make the code more readable.
This tool will help code meet the indentation rules in the WordPress coding standards.
Continue reading “Tidy assignments in Coda and TextWrangler”
Simple function to check if XML is valid. It just loads the XML into DOMDocument and checks for errors.
/** * Takes XML string and returns a boolean result where valid XML returns true */ function is_valid_xml ( $xml ) { libxml_use_internal_errors( true ); $doc = new DOMDocument('1.0', 'utf-8'); $doc->loadXML( $xml ); $errors = libxml_get_errors(); return empty( $errors ); }
Here is a function to find duplicate items in an array.
/** * Takes an array and returns an array of duplicate items */ function get_duplicates( $array ) { return array_unique( array_diff_assoc( $array, array_unique( $array ) ) ); }
Neat way of removing excess whitespace and tabbing from strings.
$string = "Tom\tThumb\t is sooooooo\t\t\tdumb"; $string = preg_replace( '/\s+/', ' ', $string ); echo $string; //will echo "Tom Thumb is sooooooo dumb"
If you want to convert comma separated values into an associated array, then use the following code.
Continue reading “Convert CSV data into an associative array”
Simple one. If you need to add/remove escape slashes to/from a string, use the following code.
Continue reading “Addslashes and stripslashes in Javascript”
If you have come across the cursed ‘Invalid Character‘ error while using PHP’s XML or JSON parser then you may be interested in this.
Continue reading “Remove non-UTF8 characters from string with PHP”
I was working on exporting CSV data to a Google spreadsheet recently. The first thing I had to do was implement a way to allow a user to authenticate themselves with Google from our site. There are a few ways of doing this.
I choose OAuth as I thought it would be simplest. I found it was pretty straightforward but there were gaps in Google’s documentation that made the process a tad frustrating.
Here’s a brief outline…
Continue reading “Export data to Google spreadsheet with OAuth”
I recently wrote a Javascript library to show a floating menu that works cross browser and displays the menu based on a set of co-ordinates that you pass it.
The floating menu is just an absolute div that is hidden from view.
The library is smart in the sense that it determines where best to display the menu. It works out where you have scrolled to on the page and figures out if there is space at the top or bottom and to the left or right of where ever your target co-ordinates are.
This is useful where you want to display a menu, or some div, but you don’t know (or care) where the menu is going to be needed on the page.
Continue reading “Create a smart, cross browser floating menu using Javascript”