从一个字符串中选择多个子字符串

时间:2019-06-05 04:53:43

标签: php arrays string

我是一个新手,并且我有一些字符串的php数组,例如{“ VAL1”,“ VAL2”,“ VAL3”,“ VAL4”},并且我有一个较大的字符串(通常是文本文件)。文本文件在不同位置包含数组的元素,某些或全部可能重复多个。文本文件中包含的数组的每个元素都紧随其后出现,例如,“ VAL1 0900UTC和其他文本信息”。。现在的问题是我想要查找文本文件中数组所有元素的出现以及该元素之后的时间值。 请注意,某些元素可能在文本文件中具有不同时间值的不同位置多次出现。 我已经可以加载文本文件并获取所有元素。:

    $mytextfile = preg_replace('/\s+/', ' ', strtoupper(file_get_contents($textpath."/". $textfile)));

    $substr = array("REALTK","BGPTK", "SUDTK", "BECTK");  
    $bigstring = "REALTK 1000UTC 16/14 1011 BGPTK 1030UTC 10/12 992 REALTK 1145UTC 00/14 2222 SUDTK 1412UTC 11/06 1011 REALTK 1600UTC 16/14 1015 ...";

    //I created variables to hold all element occuring to false  
    $is_val1 = false; $is_val2 = false;$is_val3 = false; $is_val4 = false;

    //I created variables to count how many of each substring exist in the string  
       $numofval1=$numofval2=$numofval3=$numofval4=0;  

         if(strpos($bigstring, $substr[0]) !== false) {  
            $is_val1 = true;  
         //if exist, count how many times  
        $numofval1 =  substr_count(strtoupper($bigstring),$substr[0]);
         } e.t.c  

我已经能够获得大字符串中每个数组元素的出现 我已经能够检测出是否有任何数组元素在大字符串中出现了多次,并多次出现。 但是我无法按照它们在文本文件字符串中的位置以及每个元素后的时间值的出现顺序来获取它们。

$mytextfile = preg_replace('/\s+/', ' ', strtoupper(file_get_contents($textpath."/". $textfile)));  

$substr = array("REALTK","BGPTK", "SUDTK", "BECTK");  

//这是文本文件的样本内容
    $ bigstring =“ REALTK 1000UTC 16/14 1011 BGPTK 1030UTC 10/12 992 REALTK 1145UTC 00/14 2222 SUDTK 1412UTC 11/06 1011 REALTK 1600UTC 16/14 1015 ...”;

//I created variables to hold all element occuring to false  
$is_realtk = false; $is_bgptk = false;$is_sudtk = false; $is_bectk = false;  

//I created variables to count how many of each of the element exist in the text file string    
   $numofrealtk=$numofbgptk=$numofsudtk=$numofbectk=0;  

     if(strpos($bigstring, $substr[0]) !== false) {  
        $is_realtk = true;  
     //if exist, count how many times  
    $numofrealtk =  substr_count(strtoupper($bigstring),$substr[0]);
     } e.t.c  

我需要的是按其位置和时间值在文本文件中出现的顺序获得数组的元素

REALTK POSITION1 1000UTC
BGPTK POSITION5 1030UTC  
REALTK POSITION8 1145UTC  
SUDTK POSITION13 1412UTC  
REALTK POSITION17 1600UTC  

我还想将元素=> timevalue存储为关联数组。 谢谢您。

2 个答案:

答案 0 :(得分:0)

如果$ bigstring中的元素总是被空格分隔,则可以爆炸:

$val_arr = array( "REALTK" => array(), "BGPTK" => array(), "SUDTK" => array(), "BECTK" => array();
$bigstring_arr = explode( " ", $bigstring);
foreach( $bigstring_arr as $key => $part ){
    if( in_array( $part, $substr ) ){
        $val_arr[$part][] = array( "position" => $key+1, "time" => $bigstring_arr[$key+1] );
    }
}

如果不是,那么您还可以直接在Val1,Val2 ...的每个元素上爆炸。然后,代码变得更加复杂。但是,如果您以“ CountOfAllCharsInBigstringBeforeOccurence” =>“ KeyOfValInSubstr”的形式创建了一个新数组,它也可以正常工作。从中可以得出使用val的排序。

编辑: 我根据您的评论编辑了代码。请注意,不可能有多次具有相同键的数组,这就是为什么这里的解决方案将具有多个维度的原因。

答案 1 :(得分:0)

这是您的摘录,请参见内联文档以获取说明

$substr = array("VAL1","VAL2", "VAL3", "VAL4");
$bigstring = "OTHERS VAL1 VAL4 VAL1 OTHERS OTHERS VAL2 OTHERS";
// considering space to explode words and having numbers in it
$temp = array_unique(str_word_count($bigstring, 1,"0..9"));
// sorting generated array and keeping index of value
asort($temp);
// matching records from array
$temp = array_intersect($temp,$substr);
// showing all the data you want 
array_walk($temp,function($item,$k){
    echo $item.' POSITION '.$k."\n"; // replace \n with <br/> if web
});

Working demo