Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中提取两个标记之间的文本_Php - Fatal编程技术网

如何在php中提取两个标记之间的文本

如何在php中提取两个标记之间的文本,php,Php,我需要找到一个文本块中的2个标记,并保留它们之间的任何文本 例如,如果“开始”标记是----start-----,而“结束”标记是----End----- 鉴于这一案文: rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r -----end-----gcgkhjkn 我只需要保留两个标记之间的文本:isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r 有什么想法吗?

我需要找到一个文本块中的2个标记,并保留它们之间的任何文本

例如,如果“开始”标记是
----start-----
,而“结束”标记是
----End-----

鉴于这一案文:

rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r
-----end-----gcgkhjkn
我只需要保留两个标记之间的文本:
isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r

有什么想法吗?谢谢。

试试这个:

$start = '-----start-----';
$end   = '-----end-----';
$string = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$output = strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);
echo $output;
这:

试试这个:

$start = '-----start-----';
$end   = '-----end-----';
$string = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$output = strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);
echo $output;
这:


以下是几种方法:

$lump = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$start_tag = '-----start-----';
$end_tag = '-----end-----';

// method 1
if (preg_match('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    echo $matches[1];
}

// method 2 (faster)
$startpos = strpos($lump, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
    $endpos = strpos($lump, $end_tag, $startpos);
    if ($endpos !== false) {
        echo substr($lump, $startpos, $endpos - $startpos);
    }
}

// method 3 (if you need to find multiple occurrences)
if (preg_match_all('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    print_r($matches[1]);
}

以下是几种方法:

$lump = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$start_tag = '-----start-----';
$end_tag = '-----end-----';

// method 1
if (preg_match('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    echo $matches[1];
}

// method 2 (faster)
$startpos = strpos($lump, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
    $endpos = strpos($lump, $end_tag, $startpos);
    if ($endpos !== false) {
        echo substr($lump, $startpos, $endpos - $startpos);
    }
}

// method 3 (if you need to find multiple occurrences)
if (preg_match_all('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    print_r($matches[1]);
}

如果字符串实际上是HTML数据,则必须添加htmlentities($lump),这样它就不会返回空:

$lump = '<html><head></head><body>rtyfbytgyuibg-----start-----<div>isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r</div>-----end-----gcgkhjkn</body></html>';
$lump = htmlentities($lump) //<-- HERE
$start_tag = '-----start-----';
$end_tag = '-----end-----';

// method 1
if (preg_match('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
        echo $matches[1];
}

// method 2 (faster)
$startpos = strpos($lump, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
   $endpos = strpos($lump, $end_tag, $startpos);
        if ($endpos !== false) {
            echo substr($lump, $startpos, $endpos - $startpos);
        }
}

// method 3 (if you need to find multiple occurrences)
if (preg_match_all('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
        print_r($matches[1]);
 }

// method 4 
$output = strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);

//Turn back to regular HTML
echo htmlspecialchars_decode($output);
$lump='RTYFBYUTGYUIBG----开始----ISNV4B987B6VDC5Y6UGHJMN9B8V76CTYUBINN98B76R----结束----gcgkhjkn';

$lump=htmlentities($lump)/如果字符串实际上是HTML数据,则必须添加htmlentities($lump),这样它就不会返回空:

$lump = '<html><head></head><body>rtyfbytgyuibg-----start-----<div>isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r</div>-----end-----gcgkhjkn</body></html>';
$lump = htmlentities($lump) //<-- HERE
$start_tag = '-----start-----';
$end_tag = '-----end-----';

// method 1
if (preg_match('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
        echo $matches[1];
}

// method 2 (faster)
$startpos = strpos($lump, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
   $endpos = strpos($lump, $end_tag, $startpos);
        if ($endpos !== false) {
            echo substr($lump, $startpos, $endpos - $startpos);
        }
}

// method 3 (if you need to find multiple occurrences)
if (preg_match_all('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
        print_r($matches[1]);
 }

// method 4 
$output = strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);

//Turn back to regular HTML
echo htmlspecialchars_decode($output);
$lump='RTYFBYUTGYUIBG----开始----ISNV4B987B6VDC5Y6UGHJMN9B8V76CTYUBINN98B76R----结束----gcgkhjkn';

$lump=htmlentities($lump)//你不需要在
substr
的第二个参数中用
strlen($start)
而不是
+1
来补偿吗?substr()的参数是$string、$start、$length谢谢大家的评论-刚刚测试过,现在应该可以用了。我建议存储start pos
strop($string,$start)+strlen($start))
在另一个变量中,通过
strpos($end)
减去起始位置来获取长度,但是您的
strstr
要好得多+1在
substr
的第二个参数中,你不需要用
strlen($start)
而不是
+1
来补偿吗?substr()的参数是$string,$start,$length谢谢大家的评论-刚刚测试过,现在应该可以了。我建议存储start pos($string,$start)+strlen($start))
在另一个var中,通过
strpos($end)
减去起始pos来获取长度,但是您的
strstr
要好得多+1你好,燕。。我没有尝试过任何东西,因为我不确定php函数能做什么。。但是我现在用的是preg_Matchi Yan。。我没有尝试过任何东西,因为我不确定php函数能做什么。。但是我现在正在使用preg_matchiya标记,感谢这3个im使用函数getTextBetweenTags($string){$pattern=“/----BEGIN-PKCS7----------(.*)END-PKCS7----------/“preg_match($pattern,$string,$matches);return$matches[1];}$txt=getTextBetweenTags($mytext);请参阅我对方法#2的更正,以检查是否找到结束标记。@john,我建议您像我一样使用if()语句;Hiya Mark,感谢这3个im使用函数getTextBetweenTags($string){$pattern=“/----BEGIN PKCS7----------(.*)END PKCS7----------/“preg_match($pattern,$string,$matches);返回$matches[1];}$txt=getTextBetweenTags($mytext);请参阅我对方法#2的更正,以检查是否找到结束标记。@john,我建议您像我一样使用if()语句;否则,如果没有匹配项,就会出现错误。