0

Hi,

A little stuck here - i have a file containing info like this:
Name: Some Name Email: some@email.com
I'm using file_get_contents to read the file and then i want to get the name. I have it like

preg_match('/Name:(.*?) Email:/',$filecont,$name);

however it returns nothing.
Maybe anyone has an idea how to make this work?

Thanks.

Edited by Buppy: n/a

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by Stefano Mtangoo
0

Hi,

A little stuck here - i have a file containing info like this:
Name: Some Name Email: some@email.com
I'm using file_get_contents to read the file and then i want to get the name. I have it like

preg_match('/Name:(.*?) Email:/',$filecont,$name);

however it returns nothing.
Maybe anyone has an idea how to make this work?

Thanks.

Form PHP Manual:
preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.

0
preg_match_all('/Name:(.*?) Email:/',$filecont,$name);

When checking the $name array, it contains 2 indexes (0 and 1), and both of them is another empty array. Still no name found.

0

I tried the statement with a string and it works fine using the format you indicated. Perhaps the file isn't being read in properly?

This works, for example:

<?php

$filecont ='Name: Some Name 1 Email: some@email.com
Name: Some Name 2 Email: some@email.com
Name: Some Name 3 Email: some@email.com
Name: Some Name 4 Email: some@email.com
Name: Some Name 5 Email: some@email.com
Name: Some Name 6 Email: some@email.com
';
preg_match('/Name:(.*?) Email:/',$filecont,$names);
print_r($names);

preg_match_all('/Name:(.*?) Email:/',$filecont,$names);
print_r($names);
?>
0

I got it working. It was actually the newline character (\n), which was in the file, but was not a part of the regular expression. Removing the newlines before preg_match works fine.

Thanks

Solved

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.