Parse text between 2 words

后端 未结 5 1302
慢半拍i
慢半拍i 2021-01-20 23:51

For sure this has already been asked by someone else, however I\'ve searched here on SO and found nothing https://stackoverflow.com/search?q=php+parse+between+words

I ha

5条回答
  •  一生所求
    2021-01-21 00:29

    This allows you to run the same function with different parameters, just so you don't have to rewrite this bit of code all of the time. Also uses the strpos which you used. Has been working great for me.

    function get_string_between($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }
    
    $fullstring = 'This is a long set of words that I am going to use.';
    
    $parsed = get_string_between($fullstring, 'This', "use");
    
    echo $parsed;
    

    Will output:

    is a long set of words that I am going to
    

提交回复
热议问题