Link to home
Start Free TrialLog in
Avatar of john80988
john80988

asked on

php split command

example of a string : Big Medium(20,2) Small(25,3)

1) want to redefined it to big medium small    - so it need remove all (xxxxx) and convert to small letter

2)  another is split it into array

expert pls help
ASKER CERTIFIED SOLUTION
Avatar of oheil
oheil
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SStory
Something like this..you may have to tweak it.
<?php
$inputstring="Big Medium(20,2) Small(25,3)";
$newstring="";
$instrlen=strlen($inputstring);
$inparens=false;

for ($i=1; $i<$instrlen; $i++)
  {
       //get a char
        $c=substr($inputstring,$i,1);

        //eval the char
       switch ($c) {
           case '(':
                //started the ( section; eat everything until we find a )
                $inparens=true;
                break;
           case ')':
                //found ) so stop eating chars
                $inparens=false;
                break;
           default:
                //do this otherwise
               if ($inparens) {
                   //do nothing, i.e. eat the chars
                   //this isn't necessary, but here for clarity
               }
               else {
                   //we aren't in parens so continue buiding our new string
                   $newstring=$newstring . $c;
               }
       }//switch
  }//for
?>

Open in new window


When this is done $newstring should contain the string without the ( ) and anything between them.

the following will split it at every space; you'll need to set that to an array
split(" ", $newstring);

However it says the above is deprecated.
http://php.net/manual/en/function.split.php

I highly suggest that you do a little reading when trying to do these. A good reference is found here:
http://www.php.net/manual/en/index.php
Here we go now tested:
<?php

$new=strtolower(preg_replace("/\(.*\)/U","","Big Medium(20,2) Small(25,3)"));
print($new);
$array=explode(" ",$new);
print_r($array);

?>