PHP String Function, How can I isolate what is in the last set of parens? -
i have write string function takes whatever in last parens , put new variable, without parens. in case, ups next day air.
$oldvar = 'united parcel service (1 pkg x 4.00 lbs total) (ups next day air)'; $newvar = 'ups next day air';
thanks!,
the following code sets $newvar
correct string if there matching parens in $oldvar
, false
otherwise.
$oldvar = 'united parcel service (1 pkg x 4.00 lbs total) (ups next day air)'; $poslastopeningparenthesis = strrpos($oldvar, '('); if ($poslastopeningparenthesis === false) { $newvar = false; } else { $poslastopeningparenthesis++; // move position behind opening parenthesis $poslastclosingparenthesis = strpos($oldvar, ')', $poslastopeningparenthesis); if ($poslastclosingparenthesis === false) { $newvar = false; } else { $newvar = substr($oldvar, $poslastopeningparenthesis, $poslastclosingparenthesis - $poslastopeningparenthesis); } }
Comments
Post a Comment