【问题标题】:PHP: Get string between two character with specific indexPHP:获取具有特定索引的两个字符之间的字符串
【发布时间】:2016-06-23 07:56:40
【问题描述】:

我是 PHP 的初学者。我有字符串:

$fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";

我想从我的$fullstring 获取字符串"cat"

我曾经尝试过:

Get substring between two strings PHP

但我只能得到第一个字符串(dog)。感谢您的时间。

卡特卡特。

【问题讨论】:

  • 我只想得到“猫”

标签: php


【解决方案1】:

免责声明:这两种解决方案都是幼稚的。你应该经常检查preg_match_allstrpos的返回值。

可读的正则表达式解决方案:

尝试使用preg_match_all

$fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";

preg_match_all("'\[tag\](.*?)\[\/tag\]'si", $fullstring, $match);

foreach($match[1] as $val){
    echo $val, ' ';
}
// result: dog cat lion

只获取第二个标签(现在是cat)的内容

echo $match[1][1]
// result: cat

不可读的字符串拆分解决方案:

$fullstring = 'this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]';

function get_strings_between(string $string, string $start, string $end): array {
    $r = [];
    $startLen = strlen($start);
    $endLen = strlen($end);
    while (!empty($string)) {
        $startPosition = strpos($string, $start);
        if ($startPosition === false) {
            return $r;
        }
        $contentStart = $startPosition + $startLen;
        $contentEnd = strpos($string, $end, $contentStart);
        $len = $contentEnd - $contentStart;
        $r[] = substr($string, $contentStart, $len);
        $endPosition = $contentEnd + $endLen;
        $string = substr($string, $endPosition);
    }
    return $r;
}

$match = get_strings_between($fullstring, '[tag]', '[/tag]');
// result: dog cat lion

只获取第二个标签(现在是cat)的内容

echo $match[1]
// result: cat

【讨论】:

  • 我正要发布完全相同的答案。这是我做的一个测试phpliveregex.com/p/g9V 这是稍微不同的模式,但无论如何,确切的模式可能会因 OP 的情况而有所不同。
【解决方案2】:

这是this answer 的修改版本。此函数将根据您要搜索的内容返回它可以找到的所有实例。

    function getContents($str, $startDelimiter, $endDelimiter, $needle) {
    $contents = array();
    $startDelimiterLength = strlen($startDelimiter);
    $endDelimiterLength = strlen($endDelimiter);
    $startFrom = $contentStart = $contentEnd = 0;
    while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
        $contentStart += $startDelimiterLength;
        $contentEnd = strpos($str, $endDelimiter, $contentStart);
        if (false === $contentEnd) {
            break;
        }
        $tempString = substr($str, $contentStart, $contentEnd - $contentStart);
        if ($tempString == $needle) {
            $contents[] = $tempString;
        }
        $startFrom = $contentEnd + $endDelimiterLength;
    }

    return $contents;
}

【讨论】:

  • 使用:getContents($fullstring,'[tag]','[/tag]','cat'); // 将返回字符串中所有 cat 的实例
【解决方案3】:

您正在寻找preg_match() 函数。下面的示例可能会对您有所帮助。

 $fullString = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
 $regex = "/\[tag\]\w+\[\\tag\]/";
 preg_match($regex, $fullString, $matches);
 foreach($matches as $match){
     echo $match;
 }
 // result, dog, cat, lion

【讨论】:

    【解决方案4】:

    我尝试修改旧函数 [http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/]。我得到了这个:

    function get_string_between($string, $start, $end, $index){
        if ($index <= 0) return '';
        $string = ' ' . $string;
        $ini = 0;
        $x = 1;
        while ($x <= $index) {
          $ini = strpos($string, $start, $ini + 1);
          if ($ini == 0) return '';
          $x++;
        }
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    
    $fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
    echo get_string_between($fullstring, "[tag]", "[/tag]", 2); // Result = cat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-12
      • 2013-03-16
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2012-12-28
      相关资源
      最近更新 更多