Re: Parsing Variables
| From: | Curt Zirzow | Date: | Thu, 31 Jul 2003 02:32:08 +0000 |
| Subject: | Re: Parsing Variables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-157638@lists.php.net to get a copy of this message | ||
* Thus wrote Jason Williard (jason@jasondubya.com):
> I would like to create a script that reads a file name that follows a
> specific format and have it parsed into 2 variables. The format is as
> follows:
>
> cli_info-ACCOUNT-USERNAME.dat
>
> The two variables that I would like to get out of this are the ACCOUNT and
> USERNAME. The rest of the information can be discarded. I can get the
> entire filename into a variable, but I am not sure how to parse it properly.
One method would be to use the preg_split():
list($extra, $account, $username) =
split('/[-.]/', 'cli_info-ACCOUNT-USERNAME.dat', 3);
or a a preg_match():
preg_match('/cli_info-([^-]+)-([^.]+)\.dat/', $str, $matches);
$account = $matches[1];
$username = $matches[2];
This is assumint that account wont contain a '-' and username wont
contain a '.'.
HTH,
Curt
--
"I used to think I was indecisive, but now I'm not so sure."