Solved

php split command

Posted on 2013-02-05
4
178 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 look for a specific file type in a local or remote server directory using PHP.

728 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