Extracting string...

He has: 1,758 posts

Joined: Jul 2002

Hi all,

I'm currently in the process of writing a script to parse an email that I get from my message board so that rather than having to click through emails to manually approve members, I can do it via a web page.

As part of this, I need to extract the username from the email. The username is always contained within quote marks (i.e. "username")

Does anyone know if there is a php function that will match a regular expression and give you the matched text as a variable? I thought preg_match would do the job but from what I can tell that just returns a count of how many times it matches?

Also, does anyone know what the regular expression would be?

I've tried: \"+[a-zA-Z0-9]\" (one or more alpha numeric characters) but that didnt seem to work.

What I guess I really need is a quote mark followed by up to 50 of anything then another quote mark... any ideas? I hate regex! (but seriously need to learn it!)

Andy

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

andy206uk;214983 wrote: Hi all,
Does anyone know if there is a php function that will match a regular expression and give you the matched text as a variable? I thought preg_match would do the job but from what I can tell that just returns a count of how many times it matches?

It does return only the number of matches, but preg_match() will work. It takes an argument called matches that names a variable in which the function will store an array of successfully matched strings.

This is the bizarre world of PHP, after all.

andy206uk;214983 wrote: I've tried: \"+[a-zA-Z0-9]\" (one or more alpha numeric characters) but that didnt seem to work.

You've got the + on the wrong side. Try [incode]\"[a-zA-Z0-9]+\"[/incode]. Smiling

He has: 1,758 posts

Joined: Jul 2002

ah ha! I'll get there with the regex one day! Wink

Thanks mate!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.