Solved

php split command

Posted on 2013-02-05
4
165 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
Comment Utility
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
Comment Utility
array=explode(" ",new);
is right. Mangled the parameters.

Oli
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

15 Experts available now in Live!

Get 1:1 Help Now