Emad doom
Emad doom

Reputation: 87

small question in how to cut words

<?php
    $string = 
    '--os
    windows,linux
    --os
    Mac,Linux
    --os
    Mac,windows
    ';

    $str__len = strlen('--os');
    $strr_pos = strrpos($string,'--os') + $str__len;
    $subb_str = substr($string,$strr_pos,100000);
    echo $subb_str;
    /*
    OUTPUT
    Mac,windows
    */

?>

but how to get the center and first os (Mac,Linux) and (windows,linux)? i have small idea but it stupid and slowly!

Note: The $string varibale is contain big data! it only contain three --os tag but each --os tag contain about 1000 line! So, i want get profissional code to avoid the slowing!

Thank you

Upvotes: 0

Views: 58

Answers (1)

user554546
user554546

Reputation:

If I'm understanding your question, you want to separate out the strings between the '--os' substrings. You can do this fairly easily using the explode() function:

$string = 
'--os
windows,linux
--os
Mac,Linux
--os
Mac,windows
';

$arr=explode('--os\n',$string) 

//"\n" is the newline character.  
//The elements are now '','windows,linux','Mac,linux','Mac,windows'.


//Array of the operating systems that we want:
$operating_systems=array();

//Loop through $arr:
foreach($arr as $x)
{
  if($x!='') //Ignore the silly '' element.
  {
    $sub_arr=explode(",",$x); //Make a new sub-array.
                              //so, if $x='windows,linux', 
                              //then $sub_arr has the elements "windows" and "linux".
    foreach($sub_arr as $i)
    {
      array_push($operating_systems,$i); //put these OS names into our array.
    }
  }

}

Upvotes: 1

Related Questions