Friday, 19 February 2010

Vi delete line if search does not exist

Right, if you have ever had to scroll through an endless text file looking for the needle in a haystack, there is a simpler way using the vi editor. Using vi, You can use a simple search and delete command to search the file and delete lines where a phrase doesn;t exist, like so;

:g!/text to look for/d

The example above searches all lines for text containing 'text to look for' and deletes the line if the text does not exist.

You could do the opposit and delete lines where the text exists like so;

:g/text to look for/d

Tuesday, 16 February 2010

json_decode Fatal error: Cannot use object of type stdClass as array

You may receive the following error when encoding and decoding information using json in php

Fatal error: Cannot use object of type stdClass as array

When using json_decode, information is by default converted into an object. You need to use the function get_object_vars to convert the object into an array, like so;


$new_data = get_object_vars(json_decode($data));

print_r($new_data);

?>