Solved

php split command

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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.

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now