如何在 PHP 中获得两个字符串之间的子字符串?

我需要一个返回两个单词(或两个字符)之间的子字符串的函数。 我想知道是否有一个 php 函数可以实现这一点。我不想考虑正则表达式(好吧,我可以做一个,但真的不认为这是最好的方式去)。对 strpossubstr函数的思考。 这里有一个例子: < br/>

$string = "foo I wanna a cake foo";

我们调用函数: $substring = getInnerSubstring($string,"foo");
它返回: “我想要一个蛋糕”。 < br/>


更新: 到目前为止,我只能在一个字符串中得到两个单词之间的一个子字符串,你是否允许我进一步问一下,我是否可以扩展 getInnerSubstring($str,$delim)的使用来得到任何位于分隔符值之间的字符串,例如:

$string =" foo I like php foo, but foo I also like asp foo, foo I feel hero  foo";

我得到一个类似 {"I like php", "I also like asp", "I feel hero"}的数组。

385225 次浏览

如果使用 foo作为分隔符,请查看 explode()

function getInnerSubstring($string,$delim){
// "foo a foo" becomes: array(""," a ","")
$string = explode($delim, $string, 3); // also, we only need 2 items at most
// we check whether the 2nd is set and return it, otherwise we return an empty string
return isset($string[1]) ? $string[1] : '';
}

使用示例:

var_dump(getInnerSubstring('foo Hello world foo','foo'));
// prints: string(13) " Hello world "

如果要删除周围的空白,请使用 trim。示例:

var_dump(trim(getInnerSubstring('foo Hello world foo','foo')));
// prints: string(11) "Hello world"

这是一个函数

function getInnerSubstring($string, $boundstring, $trimit=false) {
$res = false;
$bstart = strpos($string, $boundstring);
if ($bstart >= 0) {
$bend = strrpos($string, $boundstring);
if ($bend >= 0 && $bend > $bstart)
$res = substr($string, $bstart+strlen($boundstring), $bend-$bstart-strlen($boundstring));
}
return $trimit ? trim($res) : $res;
}

像这样使用它

$string = "foo I wanna a cake foo";
$substring = getInnerSubstring($string, "foo");


echo $substring;

输出(注意,如果字符串存在,它将返回字符串前面和字符串和字符串的空格)

我想吃蛋糕

如果你想修剪结果使用如下函数

$substring = getInnerSubstring($string, "foo", true);

结果 : 如果 $string中没有 $boundstring,或者 $string中只有一个 $boundstring,那么这个函数将返回 假的,否则返回 $string$boundstring第一次出现和最后一次出现之间的子串。


参考文献

如果字符串不同(例如: [ foo ] & [/foo ]) ,看看 Justin Cook 的 这篇文章。 我复制他的密码如下:

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 my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');


echo $parsed; // (result = dog)
function getBetween($string, $start = "", $end = ""){
if (strpos($string, $start)) { // required if $start not exist in $string
$startCharCount = strpos($string, $start) + strlen($start);
$firstSubStr = substr($string, $startCharCount, strlen($string));
$endCharCount = strpos($firstSubStr, $end);
if ($endCharCount == 0) {
$endCharCount = strlen($firstSubStr);
}
return substr($firstSubStr, 0, $endCharCount);
} else {
return '';
}
}

使用范例:

echo getBetween("abc","a","c"); // returns: 'b'


echo getBetween("hello","h","o"); // returns: 'ell'


echo getBetween("World","a","r"); // returns: ''
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}

例如,您希望下面@@ 之间的字符串数组(键) 例如,其中“/”不处于中间

$str = "C://@@ad_custom_attr1@@/@@upn@@/@@samaccountname@@";
$str_arr = getInbetweenStrings('@@', '@@', $str);


print_r($str_arr);
function strbtwn($s,$start,$end){
$i = strpos($s,$start);
$j = strpos($s,$end,$i);
return $i===false||$j===false? false: substr(substr($s,$i,$j-$i),strlen($start));
}

用途:

echo strbtwn($s,"<h2>","</h2>");//<h2>:)</h2> --> :)

用途:

<?php


$str = "...server daemon started with pid=6849 (parent=6848).";
$from = "pid=";
$to = "(";


echo getStringBetween($str,$from,$to);


function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}


?>

用途:

function getdatabetween($string, $start, $end){
$sp = strpos($string, $start)+strlen($start);
$ep = strpos($string, $end)-strlen($start);
$data = trim(substr($string, $sp, $ep));
return trim($data);
}
$dt = "Find string between two strings in PHP";
echo getdatabetween($dt, 'Find', 'in PHP');

来自 GarciaWebDev 和 Henry Wang 的改进代码。如果给定 $start 或 $end 为空,则函数返回从 $string 开始或结束的值。此外,包含选项可用,无论我们是否要包含搜索结果:

function get_string_between ($string, $start, $end, $inclusive = false){
$string = " ".$string;


if ($start == "") { $ini = 0; }
else { $ini = strpos($string, $start); }


if ($end == "") { $len = strlen($string); }
else { $len = strpos($string, $end, $ini) - $ini;}


if (!$inclusive) { $ini += strlen($start); }
else { $len += strlen($end); }


return substr($string, $ini, $len);
}

我得给 Julius Tilvikas 的帖子加点东西。我寻找一个解决方案,就像他在他的帖子中描述的那样。但我觉得这里有个错误。我没有得到两个字符串之间的字符串,我也得到了更多的解,因为我必须减去开始字符串的长度。这样做的时候,我得到了两个字符串之间的字符串。

以下是我对他解决方案的修改:

function get_string_between ($string, $start, $end, $inclusive = false){
$string = " ".$string;


if ($start == "") { $ini = 0; }
else { $ini = strpos($string, $start); }


if ($end == "") { $len = strlen($string); }
else { $len = strpos($string, $end, $ini) - $ini - strlen($start);}


if (!$inclusive) { $ini += strlen($start); }
else { $len += strlen($end); }


return substr($string, $ini, $len);
}

你好

V

我已经使用这个很多年了,它工作得很好,可能会更有效率,但是

Grabstring (“ Test string”,“”,“ ,0)返回 Test string
Grabstring (“ Test string”,“ Test”,“”,0)返回 string
Grabstring (“ Test string”,“ s”,“ ,5)返回 string

function grabstring($strSource,$strPre,$strPost,$StartAt) {
if(@strpos($strSource,$strPre)===FALSE && $strPre!=""){
return("");
}
@$Startpoint=strpos($strSource,$strPre,$StartAt)+strlen($strPre);
if($strPost == "") {
$EndPoint = strlen($strSource);
} else {
if(strpos($strSource,$strPost,$Startpoint)===FALSE){
$EndPoint= strlen($strSource);
} else {
$EndPoint = strpos($strSource,$strPost,$Startpoint);
}
}
if($strPre == "") {
$Startpoint = 0;
}
if($EndPoint - $Startpoint < 1) {
return "";
} else {
return substr($strSource, $Startpoint, $EndPoint - $Startpoint);
}

}

正则表达式是解决方法:

$str = 'before-str-after';
if (preg_match('/before-(.*?)-after/', $str, $match) == 1) {
echo $match[1];
}

在线 Php

如果从一个字符串有多个递归,并且有不同的[ start ]和[ end ]模式。 这是一个输出数组的函数。

function get_string_between($string, $start, $end){
$split_string       = explode($end,$string);
foreach($split_string as $data) {
$str_pos       = strpos($data,$start);
$last_pos      = strlen($data);
$capture_len   = $last_pos - $str_pos;
$return[]      = substr($data,$str_pos+1,$capture_len);
}
return $return;
}

这里使用的 get _ string _ between ()函数有一些问题。所以我带来了我自己的版本。也许这能帮到我这个案子里的人。

protected function string_between($string, $start, $end, $inclusive = false) {
$fragments = explode($start, $string, 2);
if (isset($fragments[1])) {
$fragments = explode($end, $fragments[1], 2);
if ($inclusive) {
return $start.$fragments[0].$end;
} else {
return $fragments[0];
}
}
return false;
}

写这些一段时间回来,发现它非常有用的应用范围很广。

<?php


// substr_getbykeys() - Returns everything in a source string that exists between the first occurance of each of the two key substrings
//          - only returns first match, and can be used in loops to iterate through large datasets
//          - arg 1 is the first substring to look for
//          - arg 2 is the second substring to look for
//          - arg 3 is the source string the search is performed on.
//          - arg 4 is boolean and allows you to determine if returned result should include the search keys.
//          - arg 5 is boolean and can be used to determine whether search should be case-sensative or not.
//


function substr_getbykeys($key1, $key2, $source, $returnkeys, $casematters) {
if ($casematters === true) {
$start = strpos($source, $key1);
$end = strpos($source, $key2);
} else {
$start = stripos($source, $key1);
$end = stripos($source, $key2);
}
if ($start === false || $end === false) { return false; }
if ($start > $end) {
$temp = $start;
$start = $end;
$end = $temp;
}
if ( $returnkeys === true) {
$length = ($end + strlen($key2)) - $start;
} else {
$start = $start + strlen($key1);
$length = $end - $start;
}
return substr($source, $start, $length);
}


// substr_delbykeys() - Returns a copy of source string with everything between the first occurance of both key substrings removed
//          - only returns first match, and can be used in loops to iterate through large datasets
//          - arg 1 is the first key substring to look for
//          - arg 2 is the second key substring to look for
//          - arg 3 is the source string the search is performed on.
//          - arg 4 is boolean and allows you to determine if returned result should include the search keys.
//          - arg 5 is boolean and can be used to determine whether search should be case-sensative or not.
//


function substr_delbykeys($key1, $key2, $source, $returnkeys, $casematters) {
if ($casematters === true) {
$start = strpos($source, $key1);
$end = strpos($source, $key2);
} else {
$start = stripos($source, $key1);
$end = stripos($source, $key2);
}
if ($start === false || $end === false) { return false; }
if ($start > $end) {
$temp = $start;
$start = $end;
$end = $temp;
}
if ( $returnkeys === true) {
$start = $start + strlen($key1);
$length = $end - $start;
} else {
$length = ($end + strlen($key2)) - $start;
}
return substr_replace($source, '', $start, $length);
}
?>
<?php
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
?>

例如:

<?php
$content = "Try to find the guy in the middle with this function!";
$start = "Try to find ";
$end = " with this function!";
$output = getBetween($content,$start,$end);
echo $output;
?>

这将返回“中间的家伙”。

我喜欢正则表达式解决方案,但其他方案都不适合我。

如果你知道只有一个结果,你可以使用以下方法:

$between = preg_replace('/(.*)BEFORE(.*)AFTER(.*)/s', '\2', $string);

将 BEFORE 和 AFTER 更改为所需的分隔符。

还要记住,如果没有匹配,这个函数将返回整个字符串。

这个解决方案是多行的,但是您可以根据需要使用修饰符。

echo explode('/', explode(')', $string)[0])[1];

将’/’替换为第一个字符/string,并将’)’替换为结束字符/string. :)

试试这个,它的工作为我,得到数据之间的 测试字。

$str = "Xdata test HD01 test 1data";
$result = explode('test',$str);
print_r($result);
echo $result[1];

有一些错误捕捉。具体来说,所提供的大多数函数都要求 $end 存在,而实际上在我的例子中,我需要它是可选的。Use this is $end 是可选的,如果 $start 根本不存在,则计算 FALSE:

function get_string_between( $string, $start, $end ){
$string = " " . $string;
$start_ini = strpos( $string, $start );
$end = strpos( $string, $end, $start+1 );
if ($start && $end) {
return substr( $string, $start_ini + strlen($start), strlen( $string )-( $start_ini + $end ) );
} elseif ( $start && !$end ) {
return substr( $string, $start_ini + strlen($start) );
} else {
return FALSE;
}


}

在 PHP 的 strpos样式中,如果没有找到起始标记 sm或结束标记 em,则返回 false

这个结果(false)是来自一个空字符串的 与众不同,如果开始标记和结束标记之间没有任何内容,那么就会得到这个结果。

function between( $str, $sm, $em )
{
$s = strpos( $str, $sm );
if( $s === false ) return false;
$s += strlen( $sm );
$e = strpos( $str, $em, $s );
if( $e === false ) return false;
return substr( $str, $s, $e - $s );
}

该函数将只返回第一个匹配项。

这是显而易见的,但值得一提的是,该函数将首先查找 sm,然后再查找 em

这意味着,如果必须首先搜索 em,然后在搜索 sm时必须反向解析字符串,那么可能得不到所需的结果/行为。

不是职业选手。但我最近也碰到了这堵墙,这就是我想到的。

function tag_contents($string, $tag_open, $tag_close){
foreach (explode($tag_open, $string) as $key => $value) {
if(strpos($value, $tag_close) !== FALSE){
$result[] = substr($value, 0, strpos($value, $tag_close));;
}
}
return $result;
}


$string = "i love cute animals, like [animal]cat[/animal],
[animal]dog[/animal] and [animal]panda[/animal]!!!";


echo "<pre>";
print_r(tag_contents($string , "[animal]" , "[/animal]"));
echo "</pre>";


//result
Array
(
[0] => cat
[1] => dog
[2] => panda
)

使用 strstr php 函数两次。

$value = "This is a great day to be alive";
$value = strstr($value, "is"); //gets all text from needle on
$value = strstr($value, "be", true); //gets all text before needle
echo $value;

产出: "is a great day to"

UTF-8版本的@Alejandro Iglesias 回答,将适用于非拉丁字符:

function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = mb_strpos($string, $start, 0, 'UTF-8');
if ($ini == 0) return '';
$ini += mb_strlen($start, 'UTF-8');
$len = mb_strpos($string, $end, $ini, 'UTF-8') - $ini;
return mb_substr($string, $ini, $len, 'UTF-8');
}


$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');


echo $parsed; // (result = dog)

这就是我要用到的函数。我将两个答案组合在一个函数中,用于单个或多个分隔符。

function getStringBetweenDelimiters($p_string, $p_from, $p_to, $p_multiple=false){
//checking for valid main string
if (strlen($p_string) > 0) {
//checking for multiple strings
if ($p_multiple) {
// getting list of results by end delimiter
$result_list = explode($p_to, $p_string);
//looping through result list array
foreach ( $result_list AS $rlkey => $rlrow) {
// getting result start position
$result_start_pos   = strpos($rlrow, $p_from);
// calculating result length
$result_len         =  strlen($rlrow) - $result_start_pos;


// return only valid rows
if ($result_start_pos > 0) {
// cleanying result string + removing $p_from text from result
$result[] =   substr($rlrow, $result_start_pos + strlen($p_from), $result_len);
}// end if
} // end foreach


// if single string
} else {
// result start point + removing $p_from text from result
$result_start_pos   = strpos($p_string, $p_from) + strlen($p_from);
// lenght of result string
$result_length      = strpos($p_string, $p_to, $result_start_pos);
// cleaning result string
$result             = substr($p_string, $result_start_pos+1, $result_length );
} // end if else
// if empty main string
} else {
$result = false;
} // end if else


return $result;




} // end func. get string between

简单使用(返回两个) :

$result = getStringBetweenDelimiters(" one two three ", 'one', 'three');

用于将表中的每一行转换为结果数组:

$result = getStringBetweenDelimiters($table, '<tr>', '</tr>', true);

我吸毒

if (count(explode("<TAG>", $input))>1){
$content = explode("</TAG>",explode("<TAG>", $input)[1])[0];
}else{
$content = "";
}

将 < TAG > 替换为所需的任何分隔符。

亚历杭德罗的回答的改进。可以保留 $start$end参数为空,它将使用字符串的起始或结束。

echo get_string_between("Hello my name is bob", "my", ""); //output: " name is bob"


private function get_string_between($string, $start, $end){ // Get
if($start != ''){ //If $start is empty, use start of the string
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
}
else{
$ini = 0;
}


if ($end == '') { //If $end is blank, use end of string
return substr($string, $ini);
}
else{
$len = strpos($string, $end, $ini) - $ini; //Work out length of string
return substr($string, $ini, $len);
}
}

亚历杭德罗 · 加西亚 · 伊格莱西亚斯所说的经过编辑的版本。

这允许您根据找到结果的次数选择要获取的字符串的特定位置。

function get_string_between_pos($string, $start, $end, $pos){
$cPos = 0;
$ini = 0;
$result = '';
for($i = 0; $i < $pos; $i++){
$ini = strpos($string, $start, $cPos);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
$result = substr($string, $ini, $len);
$cPos = $ini + $len;
}
return $result;
}

用途:

$text = 'string has start test 1 end and start test 2 end and start test 3 end to print';


//get $result = "test 1"
$result = $this->get_string_between_pos($text, 'start', 'end', 1);


//get $result = "test 2"
$result = $this->get_string_between_pos($text, 'start', 'end', 2);


//get $result = "test 3"
$result = $this->get_string_between_pos($text, 'start', 'end', 3);

Strpos 有一个额外的可选输入,用于在特定点开始搜索。因此我将前一个位置存储在 $cPos 中,所以当 for 循环再次检查时,它将从停止的位置的末尾开始。

这里绝大多数的答案都没有回答编辑过的部分,我猜他们是之前添加的。正如一个答案所提到的,可以使用 regex 来完成。我有不同的方法。


这个函数搜索 $string 并查找从 $offest 位置开始的 中间的第一个字符串 $start 和 $end 字符串。然后,它更新 $偏移位置以指向结果的开始。如果 $include-Dlimiters 为 true,则它将分隔符包含在结果中。

如果没有找到 $start 或 $end 字符串,则返回 null。如果 $string、 $start 或 $end 是空字符串,它也返回 null。

function str_between(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): ?string
{
if ($string === '' || $start === '' || $end === '') return null;


$startLength = strlen($start);
$endLength = strlen($end);


$startPos = strpos($string, $start, $offset);
if ($startPos === false) return null;


$endPos = strpos($string, $end, $startPos + $startLength);
if ($endPos === false) return null;


$length = $endPos - $startPos + ($includeDelimiters ? $endLength : -$startLength);
if (!$length) return '';


$offset = $startPos + ($includeDelimiters ? 0 : $startLength);


$result = substr($string, $offset, $length);


return ($result !== false ? $result : null);
}

下面的函数查找 在两个字符串之间的所有字符串(没有重叠)。它需要前面的函数,并且参数是相同的。执行后,$offest 指向最后找到的结果字符串的开始。

function str_between_all(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): ?array
{
$strings = [];
$length = strlen($string);


while ($offset < $length)
{
$found = str_between($string, $start, $end, $includeDelimiters, $offset);
if ($found === null) break;


$strings[] = $found;
$offset += strlen($includeDelimiters ? $found : $start . $found . $end); // move offset to the end of the newfound string
}


return $strings;
}

例子:

str_between_all('foo 1 bar 2 foo 3 bar', 'foo', 'bar')给出 [' 1 ', ' 3 ']

str_between_all('foo 1 bar 2', 'foo', 'bar')给出 [' 1 ']

str_between_all('foo 1 foo 2 foo 3 foo', 'foo', 'foo')给出 [' 1 ', ' 3 ']

str_between_all('foo 1 bar', 'foo', 'foo')给出 []

使用这个小函数可以很容易地完成:

function getString($string, $from, $to) {
$str = explode($from, $string);
$str = explode($to, $str[1]);
return $s[0];
}
$myString = "<html>Some code</html>";
print getString($myString, '<html>', '</html>');


// Prints: Some code

考虑一下这个函数,它同时接受位置号和字符串参数:

function get_string_between($string, $start = '', $end = ''){
if (empty($start)) {
$start = 0;
}elseif (!is_numeric($start)) {
$start = strpos($string, $start) + strlen($start);
}


if (empty($end)) {
$end = strlen($string);
}elseif (!is_numeric($end)) {
$end = strpos($string, $end);
}


return substr($string, $start, ($end - $start));
}

结果:

echo get_string_between($string); // result = this is my [tag]dog[/tag]
echo get_string_between($string, 0); // result = this is my [tag]dog[/tag]
echo get_string_between($string, ''); // result = this is my [tag]dog[/tag]
echo get_string_between($string, '[tag]'); // result = dog[/tag]
echo get_string_between($string, 0, '[/tag]'); // result = this is my [tag]dog
echo get_string_between($string, '', '[/tag]'); // result = this is my [tag]dog
echo get_string_between($string, '[tag]', '[/tag]'); // result = dog
echo get_string_between($string, '[tag]', strlen($string)); // dog[/tag]
private function getStringBetween(string $from, string $to, string $haystack): string
{
$fromPosition = strpos($haystack, $from);
$toPosition = strpos($haystack, $to, $fromPosition);
$betweenLength = $toPosition - $fromPosition;
return substr($haystack, $fromPosition, $betweenLength);
}
function img($n,$str){


$first=$n."tag/";
$n+=1;
$last=$n."tag/";
$frm = stripos($str,$first);
$to = stripos($str,$last);
echo $frm."<br>";
echo $to."<br>";
$to=($to=="")?(strlen($str)-$frm):($to-$frm);
$final = substr($str,$frm,$to);
echo $to."<br>";
echo $final."<br>";


}




$str = "1tag/Ilove.php2tag/youlike.java3tag/youlike.php";
img(1,$str);


img(2,$str);


img(3,$str);

获取特定的文本,然后在 php 中输入一个数组:

<!DOCTYPE html>
<html>
<body>


<?php
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 = '.layout {


color: \{\{ base_color }}


}


li {


color: \{\{ sub_color }}


}


.text {


color: \{\{ txt_color }}


}


.btn {


color: \{\{ btn_color }}


}


.more_text{


color:\{\{more_color}}


}';




$arr = [];


while(1){
$parsed = get_string_between($fullstring, '\{\{', '}}');
if(!$parsed)
break;
array_push($arr,$parsed);
$strposition = strpos($fullstring,"}}");
$nextString = substr($fullstring, $strposition+1, strlen($fullstring));
$fullstring = $nextString;
echo "<br>";
}
print_r($arr);


?>


</body>
</html>

没有数组:

<?php
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 = '.layout {


color: \{\{ base_color }}


}


li {


color: \{\{ sub_color }}


}


.text {


color: \{\{ txt_color }}


}


.btn {


color: \{\{ btn_color }}


}


.more_text{


color:\{\{more_color}}


}';




while(1){
$parsed = get_string_between($fullstring, '\{\{', '}}');
if(!$parsed)
break;
echo $parsed;
$strposition = strpos($fullstring,"}}");
$nextString = substr($fullstring, $strposition+1, strlen($fullstring));
$fullstring = $nextString;
echo "<br>";
}


?>

对于单个输出,删除数组和循环。

<?php
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 = '.layout {


color: \{\{ base_color }}


}


li {


color: \{\{ sub_color }}


}


.text {


color: \{\{ txt_color }}


}


.btn {


color: \{\{ btn_color }}


}


.more_text{


color:\{\{more_color}}


}';


$parsed = get_string_between($fullstring, '\{\{', '}}');
echo $parsed;


?>

简单,短暂,甜蜜,任何改进都取决于你。

function getStringBetween($str, $start, $end)
{
$pos1 = strpos($str, $start);
$pos2 = strpos($str, $end);
return substr($str, $pos1+1, $pos2-($pos1+1));
}

使用 subr 的简易解决方案

$posStart = stripos($string, $start) + strlen($start);
$length = stripos($string, $end) - $posStart;


$substring = substr($string,  $posStart,  $length);

好的,对于我来说只有3行,但是你必须启动子字符串的左边界和右边界。

function getInnerSubstring($substring,$left,$right){
$string = explode($left,$substring)[1];
return explode($right,$string)[0];
}