PHP "preg_replace" issue
by TBotNik from LinuxQuestions.org on (#5CZW1)
All,
Processing a file with strings of:
Code: $oline = preg_replace('!\s\s+!', ', ', $line);
$r_ray = explode(", ", $oline);Which works most of the time, but sometimes the "State" does not parse, but stays in $r_ray[2], leaving $r_ray[3] blank or empty!
Can I fix my preg_replace to always work or do I need an "if" statement to check for blank/empty $r_ray[3]?
Example Failed Output:
Input:
Code:Binary Capital http://www.binarycap.com/ San Francisco CAOutput:
Code:Array
(
[0] => Binary Capital
[1] => http://www.binarycap.com/
[2] => San Francisco CA
)The proper working would always put the state, "CA" in this case, into $r_ray[3]. This is working over 75% of the time and not understanding why the anomalies.
I'm processing this file to create MySQL "INSERT" statements to process into my DB. Want the SQL to be correct!
All help appreciated!
Cheers!
TBNK


Processing a file with strings of:
- Company Name
- URL
- City
- State
Code: $oline = preg_replace('!\s\s+!', ', ', $line);
$r_ray = explode(", ", $oline);Which works most of the time, but sometimes the "State" does not parse, but stays in $r_ray[2], leaving $r_ray[3] blank or empty!
Can I fix my preg_replace to always work or do I need an "if" statement to check for blank/empty $r_ray[3]?
Example Failed Output:
Input:
Code:Binary Capital http://www.binarycap.com/ San Francisco CAOutput:
Code:Array
(
[0] => Binary Capital
[1] => http://www.binarycap.com/
[2] => San Francisco CA
)The proper working would always put the state, "CA" in this case, into $r_ray[3]. This is working over 75% of the time and not understanding why the anomalies.
I'm processing this file to create MySQL "INSERT" statements to process into my DB. Want the SQL to be correct!
All help appreciated!
Cheers!
TBNK