Find Leading Zeros in exported data

I’m using a nice little generic export script to dump table data and generate a CSV file. When opening the file in Excel, leading zeros are removed. I learned that there is a trick to forcing Excel to preserve the leading zeros (found here: CSV Comma Separated Value File Format - How To - Creativyst - Explored,Designed,Delivered.(sm)).

I’d like to know how I could find a piece of data, in a very long, comma separated string, that has a leading zero. I’m using str_replace for a few things, like to remove "\r
" from the data like this:

$value = str_replace("\\r\
", "", $value);

Is there something I can use to find data with a leading zero and treat it?

I’d probably check if the value was numeric, if so, then add leading zeros to the required length,

I don’t see how I can check if the value is numeric, when the string is full of all kinds of data (names, addresses, dates)? Also, I don’t need to add zeros, the CSV creation doesn’t remove the zeros. It’s when the program is opened in Excel that they are stripped.

I need a way to search through a very long, comma separated string, and look for pieces of data that start with zero.


$arr[0] = "this is a string with a phone number 0111 222 333";
$arr[1] = "this is a string with a a left padded number 00000333";

What do you want the resulting array values to contain given those two cases once the zeros have been replaced?

Thanks for the responses. I figured out that the issue is NOT with my export of the data to CSV. The leading zeros are only removed when the file is opened in Excel. This is an issue with Excel, and not with the data. So the workaround needs to happen in Excel when the file is opened.