2011-09-09 4 views
0
<?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 
    */ 

?> 

しかし、中心と最初のOS(Mac、Linux)と(Windows、Linux)を取得する方法は? 私は小さなアイデアを持っていますが、それは愚かでゆっくりです!言葉を切る方法の小さな質問

注: $ string varibaleには大きなデータが含まれています。 これには3つの--osタグ が含まれていますが、各--osタグには約1000行が含まれています! だから、私は減速を避けるためにprofissionalコードを取得したい!私はあなたの質問を理解していた場合

はあなたに

答えて

1

をありがとう、あなたは'--os'ストリング間の文字列を分離します。 explode()関数を使用すると、これを簡単に行うことができます。

$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. 
    } 
    } 

}