用php获取两个参数之间的所有子串

时间:2018-06-14 07:39:59

标签: php

我正在尝试创建一个函数来获取两个参数之间的所有子字符串。 到目前为止,我没有成功实现这一目标。 这是我目前的代码。

<?php
function get_string_between($string, $start, $end){


    $times = substr_count($string, $start) -1; 
    $storedsubstrs = array(); 
    foreach(range(0, $times) as $number){ 
        $ini = strpos($string, $start); 
        $len = strpos($string, $end, $ini) + strlen($end); 
        $storedsubstrs += array($number => trim(substr($string, $ini, $len))); 
        $string = substr_replace($string, "", $ini, $len); 
    }

    return $storedsubstrs; 

}

$fullstring = '{{ IMPORT header.html %}}

{{ FOR %}}
s
{{ ENDFOR %}}

{{ IMPORT footer.html %}}';
echo "<pre>";
$parsed = get_string_between($fullstring, '{{ ', ' %}}');

print_r($parsed); 

但这不起作用正在输出

Array
(
[0] => {{ IMPORT header.html %}}
[1] => {{ FOR %}}
s
[2] => {{ ENDFOR %}}

{
[3] => { IMPORT footer.html %}}
)

我想要获得的输出是

 Array
(
[0] => {{ IMPORT header.html %}}
[1] => {{ FOR %}}
[2] => {{ ENDFOR %}}
[3] => {{ IMPORT footer.html %}}
)

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

你可以这样做:

$fullstring = '{{ IMPORT header.html %}}

{{ FOR %}}
s
{{ ENDFOR %}}

{{ IMPORT footer.html %}}';
$re = '/{{(.*?)%}}/';
$str = '{{fgdfg%}}';

preg_match_all($re, $fullstring, $matches );
echo "<pre>";
print_r($matches[0]);
echo "</pre>";

答案 1 :(得分:0)

我找到了这个解决方案

<?php
function substrs_between($start, $end, $string){
    $arrayhold = array();
    $loopammount = substr_count($string, $start) - 1;
    foreach(range(0, $loopammount) as $count){

        if(isset($my_start_pos)){
            $my_start_pos = strpos($string, $start, $my_start_pos + strlen($start));
        } else {
            $my_start_pos = strpos($string, $start);
        }
        if(isset($my_end_pos)){
            $my_end_pos = strpos($string, $end, $my_end_pos) + strlen($end);
            $my_end_lenth = $my_end_pos - $my_start_pos; 
        } else {
            $my_end_pos = strpos($string, $end) + strlen($end);
            $my_end_lenth = $my_end_pos - $my_start_pos;
        }


        $arrayhold[$count] = substr($string, $my_start_pos, $my_end_lenth);
    }
    return $arrayhold;
}

$mystr = "{{ FOR }} sompthing {{ ENDFOR }} {{ FOR }} sompthing2 {{ ENDFOR }}";
$my_start = "{{ FOR }}";
$my_end = "{{ ENDFOR }}";
echo "<pre>";
print_r(substrs_between($my_start, $my_end , $mystr));

您可以将$ my_start和my $ my_end更改为任何内容,它们之间会有一个子字符串

答案 2 :(得分:0)

我详细介绍了Hanan F的答复,内容涉及如何获取第一个和第二个标签之间的所有子字符串。基本上,我在回复中包括了插入或排除标记的可能性。到目前为止,您只能获得带有标记的回复。 在将该功能真正公开之前,可能需要进行一些错误检查-例如:marker1是否位于marker2之前,否则,会发生什么?但是对于可靠的消息源,以下方法应该起作用:

<?php
$yourstart = "1";
$yourend = "2";
$yourstring = "Lots of 1 things 2 happen between 1 and 2. I don't remember if 1 as well as 2 are prime, 1 maybe not, but 2? Dunno.";

echo "<br>First with markers:<br>";
echo $yourstring;
$some_ar = substrs_between($yourstart, $yourend, $yourstring, true);
echo "<pre>";
print_r($some_ar);
echo "</pre>";

echo "<br>And now without the markers: <br>";
$some_ar = substrs_between($yourstart, $yourend, $yourstring, false);
echo "<pre>";
print_r($some_ar);
echo "</pre>";

$your2start = "<b>";
$your2end = "</b>";
$your2string = "A lot of <b>bold</b> in <b>this</b> sentence. Better <b>I get rid of it.</b>";
echo "Text between markup tags:";
echo "<br>" . $your2string ."<br>";
$some_ar = substrs_between($your2start, $your2end, $your2string, false);
echo "<pre>";
print_r($some_ar);
echo "</pre>";

function substrs_between($start, $end, $string, $withmarkers) {
$arrayhold = array();
$loopamount = substr_count($string, $start) - 1;
foreach(range(0, $loopamount) as $count) {

if (isset($my_start_pos)) {
    $my_start_pos = strpos($string, $start, $my_start_pos + strlen($start));
} else {
    $my_start_pos = strpos($string, $start);
}

if (isset($my_end_pos)) {
    $my_end_pos = strpos($string, $end, $my_end_pos) + strlen($end);
    $my_end_length = $my_end_pos - $my_start_pos;
} else {
    $my_end_pos = strpos($string, $end) + strlen($end);
    $my_end_length = $my_end_pos - $my_start_pos;
}

if ($withmarkers) {
    $arrayhold[$count] = substr($string, $my_start_pos, $my_end_length);
} else {
    $arrayhold[$count] = substr($string, $my_start_pos + strlen($start),  $my_end_length - strlen($start) - strlen($end));
}   
}
return $arrayhold;
}
?>