Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
1027
Q:

PHP fscanf() function (with example)

Hi everyone,

I am currently working on a PHP project and I came across the fscanf() function. I have read the official PHP documentation about it but I'm still having some trouble understanding how it works. I would really appreciate it if someone could provide me with a clear explanation and perhaps an example of how to use it properly.

To provide some context, I'm working on a script that needs to read information from a file. I know that fscanf() is used to scan input from a file according to a specified format, but I'm not entirely sure how to correctly define that format.

For example, let's say I have a file called "data.txt" that contains the following lines:

John Doe,25,New York
Jane Smith,30,Los Angeles

I would like to use fscanf() to extract the name, age, and city from each line. How would I go about doing that? Any help or insights would be greatly appreciated.

Thank you in advance for your assistance!

Best regards,
[Your Name]

All Replies

elyse69

Hey [Your Name],

I've used fscanf() before and I can definitely help you out with this!

To use fscanf() for your example, you need to define the format you want to match. In this case, you can specify that you want to read a string, followed by an integer, followed by another string.

Here's how you could handle the "data.txt" file in your scenario:

php
$file = fopen("data.txt", "r");

while($line = fscanf($file, "%[^,],%d,%[^,\n]")) {
list($name, $age, $city) = $line;
echo "Name: " . $name . ", Age: " . $age . ", City: " . $city . "\n";
}

fclose($file);


In the fscanf() function, the format "%[^,],%d,%[^,\n]" is used. Let me break it down for you:

- "%[^,]" matches and stores a string until it encounters a comma (`,`)
- "," matches the comma itself
- "%d" matches and stores an integer (age in this case)
- "," matches another comma
- "%[^,\n]" matches and stores a string until it encounters either a comma or a new line character

By using this format, you can extract the name, age, and city values from each line in the "data.txt" file. The list() function is then used to assign these values to individual variables for further processing or display.

I hope this explanation clears things up for you! Let me know if you have any more questions.

Best regards,
[Your Name]

oliver.bartell

Hey there,

I've worked with fscanf() in one of my projects and I'd love to share my experience with you!

In your case, to extract the name, age, and city from each line in the "data.txt" file, you can use a slightly different approach with fscanf(). Here's an example:

php
$file = fopen("data.txt", "r");

while (!feof($file)) {
$line = fgets($file);
$result = sscanf($line, "%[^,],%d,%[^,\n]");

if ($result !== false) {
list($name, $age, $city) = $result;
echo "Name: " . $name . ", Age: " . $age . ", City: " . $city . "\n";
}
}

fclose($file);


Instead of using fscanf(), we can utilize fgets() to read a line from the file, and then apply sscanf() to parse the line according to the specified format.

The format string "%[^,],%d,%[^,\n]" functions similarly to the previous response. It matches and stores a string until a comma is found, followed by an integer, and then another string until a comma or a newline character is encountered.

By using sscanf(), we can extract the name, age, and city values into separate variables. If a match is found, we can proceed with further processing or displaying the extracted data.

Feel free to give it a shot in your project, and don't hesitate to ask if you have any more queries!

Best regards,
[Your Name]

New to LearnPHP.org Community?

Join the community