e.e
e.e

Reputation: 85

Regex preg_match word

I'm trying to get a part of a string that starts with for example Name:. If the whole string looks like Name: Carl, I just want the Carl part and not the Name: prefix.

How can I do that? I have tried with:

$data = file_get_contents('page.html');
$regex = '/Name:.*/';
preg_match($regex,$data,$match);
var_dump($match);

But I get the output:

array(1) { [0]=> string(28) "Name: Carl"

The other thing I don't understand is why the array(1) { [0]=> string(28) is showing.

Upvotes: 1

Views: 187

Answers (2)

Peter Adrian
Peter Adrian

Reputation: 297

You have to put what you want to retrieve in ():

'/Name:(.*)/i'

Upvotes: 2

RonaldBarzell
RonaldBarzell

Reputation: 3830

For your match line, do the following instead:

$regex = '/Name:(.*)/'; 

The matched portion (inside (.*)) will be in $match.

Upvotes: 1

Related Questions