Will Chat take over coders' jobs? Well, check out what ChatGPT-4 was able to generate. No editing. Any bugs? You be the judge.
Chat Prompt: write a php solution for parsing text columns from a csv file to find phone numbers
ChatGPT4 Output:
To parse a CSV file and find phone numbers within the text columns, you can use the following PHP script. This script uses regular expressions to identify phone numbers in the file:
<?php
// Define the file path
$csvFile = 'path/to/your/csvfile.csv';
// Open the file
if (($handle = fopen($csvFile, 'r')) !== false) {
// Define the phone number regex pattern
$phoneNumberPattern = '/\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/';
// Loop through each row
while (($row = fgetcsv($handle, 0, ',')) !== false) {
// Loop through each column
foreach ($row as $column) {
// Check if the column contains a phone number
if (preg_match_all($phoneNumberPattern, $column, $matches)) {
// Print the phone numbers found
foreach ($matches[0] as $phoneNumber) {
echo "Phone number found: " . $phoneNumber . PHP_EOL;
}
}
}
}
// Close the file
fclose($handle);
} else {
echo "Error: Unable to open the CSV file." . PHP_EOL;
}
?>
This script will search for phone numbers in each text column of the provided CSV file and print them out. Make sure to replace the $csvFile
variable with the correct path to your CSV file. Note that the regular expression provided might not cover all possible phone number formats, so you may need to adjust it according to your needs.