Probably simple php regex
Sorry for the simple question that I could research, but i crashed a database today, been here 12 hours, and want to go home.
I am rotating recursively through files trying to extract city, phone number, and email address so that I can match the city and phone to my database entries and update the users email address. In theory, they could just login with their email and request to reset t开发者_开发知识库heir password.
heres what i need. my file contents look like this:
> Address : 123 main street City : somecity State/Province : somestate
> Zip/Postal Code : 12345 Country : United States Phone : 1231231234 Fax
> : E-Mail : example@example.com ==== CUSTOMER SHIPPING INFORMATION ===
I should note that there is other info before and after the snippet I showed. Can someone please help me with a regex to remove the 3 items? Thanks.
Try something like this, without regex..
$string = 'Address : 123 main street City : somecity State/Province : somestate Zip/Postal Code : 12345 Country : United States Phone : 1231231234 Fax : E-Mail : example@example.com ==== CUSTOMER SHIPPING INFORMATION ===';
$string = str_replace(
array(
' ==== CUSTOMER SHIPPING INFORMATION ===',
'Address',
'City',
'State/Province',
'Zip/Postal Code',
'Country',
'Phone',
'Fax',
'E-Mail'
)
, '', $string);
$string = explode(' : ', $string);
unset($string[0]);
print_r($string);
Result...
Array
(
[0] =>
[1] => 123 main street
[2] => somecity
[3] => somestate
[4] => 12345
[5] => United States
[6] => 1231231234
[7] =>
[8] => example@example.com
)
If there are linebreaks, something like this...
$string = explode("\n", $string);
foreach($string as $value){
list(, $info) = explode(' : ', $value);
echo $info . '<br />';
}
Solution with regex..
$fields = array('City', 'Phone', 'E-mail');
foreach($fields as $field){
preg_match("#$field : (.*?) #is", $string, $matches);
echo "$field : $matches[1]";
echo '<br />';
}
Result:
City : somecity
Phone : 1231231234
E-mail : example@example.com
Something like this:
Address\s*:\s*(.*?)\s*City\s*:\s*(.*?)\s*State/Province\s*:\s*(.*?)\s*Zip/Postal Code\s*:\s*(.*?)\s*Country\s*:\s*(.*?)\s*Phone\s*:\s*(.*?)\s*Fax\s*:\s*(.*?)\s*E-Mail\s*:\s*(.*?)\s
Will work if you rip out the >
at the start of each line first.
proof
If you print_r
that, you'll see the different components.
Note the "dot matches all" modifier. Might be even easier if you rip out newlines too (after you take out the >
).
What about this
$test =
'
> Address : 123 main street City : somecity State/Province : somestate
> Zip/Postal Code : 12345 Country : United States Phone : 1231231234 Fax
> : E-Mail : example@example.com ==== CUSTOMER SHIPPING INFORMATION ===
';
preg_match('@.*City : (.+?) .*? Country : (.+?) Phone : (.+?) .*@si',$test,$res);
var_dump($res);
result
array
0 => string '
> Address : 123 main street City : somecity State/Province : somestate
> Zip/Postal Code : 12345 Country : United States Phone : 1231231234 Fax
> : E-Mail : example@example.com ==== CUSTOMER SHIPPING INFORMATION ===
' (length=217)
1 => string 'somecity' (length=8)
2 => string 'United States' (length=13)
3 => string '1231231234' (length=10)
精彩评论