This is a discussion on Parsing Variables within the PHP General forums, part of the PHP Programming Forums category; I would like to create a script that reads a file name that follows a specific format and have it ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. Any assistance would be appreciated. Thanks, Jason |
|
|||
|
* 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." |