/    Sign up×
Community /Pin to ProfileBookmark

Parsing string with php – preferably without regex engine

Hi, I’ve got a unique problem here – I’m very new to php and am trying to solve a problem where I need to parse the output of a few command line commands to be used in some code. I have this working, but there’s got to be a better solution.

Here’s a few example strings:
R1_testing_client: s(app1), v(1.0.0), e(dev_int), x(HA), z(APPUSERHA)
R1_testing_client: s(ny_app23), e(alpha), v(2.1), z(CUGATEWAY)
R1_testing_client: s(director), z(CUGATEWAY), e(beta2), v(3.1.4.a)

I need to parse out the s(*), v(*), e(*), x(*), z(*) values and store them as vars if they exist. everything to the left of the colon in R1_testing_client will always be the same, it’s only the values to the right of the colon that change. My solution is to split/trim my way to getting the contents of each value, but if the values come in out of order like they do above, it breaks my code. Any suggestions are appreciated.

Here’s what i have to get the s(*) value for example. As you can tell, if s(*) comes anywhere other than the first element, it breaks my code.

[CODE]
$splitDescr = explode(“:”, $description);
$splitDescr = explode(“,”, $splitDescr[1]);
$appname = substr(trim($splitDescr[0]), 2, -1);
[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@mcruauthorOct 31.2011 — I actually came up with a slightly better solution, but there's got to be a better way...

[CODE]
$splitDescr = explode(":", $description);
$splitDescr = explode(",", $splitDescr[1]);
foreach($splitDescr as $descrItem){
trim($descrItem);
if ($descrItem[0] == "s"){
$appname = substr($descrItem, 2, -1);
}
if ($descrItem[0] == "z"){
$zonename = substr($descrItem, 2, -1);
}
}
[/CODE]
Copy linkTweet thisAlerts:
@NogDogNov 01.2011 — Well, I know you said no regexp, but...
[code=php]
<?php

$test = 'R1_testing_client: s(app1), v(1.0.0), e(dev_int), x(HA), z(APPUSERHA)';
preg_match_all('#[:,]s*(w)((.*?))#', $test, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
switch($match[1]) {
case 's':
$appname = $match[2];
break;
case 'z':
$zonename = $match[2];
break;
}
}
echo "app: $appname, zone: $zonename";
[/code]
Copy linkTweet thisAlerts:
@mcruauthorNov 03.2011 — Hey thanks, I like the solution but I've had some trouble with regex and performance in php. I'm probably implementing it wrong, but I've noticed greatly improved page load times when dropping regex and using built in string functions. Everything I work on is business automation apps and it seems like I'm always parsing huge logs and displaying results to users...
×

Success!

Help @mcru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...