Solved

php split command

Posted on 2013-02-05
4
175 Views
Last Modified: 2013-02-05
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
0
Comment
Question by:john80988
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 38854908
new=strtolower(preg_replace("/\(.*\)/","","Big Medium(20,2) Small(25,3)"));

array=explode(new," ");

The use of split is deprecated.
Can you give the first a test? I cant test it from here right now.

Oli
0
 
LVL 9

Assisted Solution

by:oheil
oheil earned 500 total points
ID: 38854925
array=explode(" ",new);
is right. Mangled the parameters.

Oli
0
 
LVL 25

Expert Comment

by:SStory
ID: 38854969
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
0
 
LVL 9

Expert Comment

by:oheil
ID: 38855010
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);

?>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question