Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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_Preg Match - Fatal编程技术网

Php 获取两个标记之间的文本

Php 获取两个标记之间的文本,php,preg-match,Php,Preg Match,如何从以下标记中获取值: {desc=1} This is a description {/desc} {desc=1}中的数字正在更改。我也想得到这个值 更新: 例如,字符串中也可能有更多描述 {desc=1} This is a description {/desc} {desc=2} other description {/desc} ... 这将捕获您想要的一切 $data = <<<EOT {desc=1} This is a descr

如何从以下标记中获取值:

{desc=1}
This is a description
{/desc}
{desc=1}中的数字正在更改。我也想得到这个值

更新:

例如,字符串中也可能有更多描述

{desc=1}
    This is a description
{/desc} 
{desc=2}
    other description
{/desc}
...

这将捕获您想要的一切

$data = <<<EOT
{desc=1}
    This is a description
{/desc}
{desc=2}
    other description
{/desc}
EOT;

preg_match_all('#{desc=(\d+)}(.*?){/desc}#s', $data, $matches);

var_dump($matches);

这将捕获您想要的一切

$data = <<<EOT
{desc=1}
    This is a description
{/desc}
{desc=2}
    other description
{/desc}
EOT;

preg_match_all('#{desc=(\d+)}(.*?){/desc}#s', $data, $matches);

var_dump($matches);
试试这个

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

$str = "{attr1}/{attr2}/{attr3}";
$str_arr = getInbetweenStrings('{', '}', $str);

print_r($str_arr);
在上面的例子中,{和}是标记,在它们之间提取字符串。

试试这个

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

$str = "{attr1}/{attr2}/{attr3}";
$str_arr = getInbetweenStrings('{', '}', $str);

print_r($str_arr);

在上面的例子中,{和}是标记,在它们之间提取字符串。

另一个非常简单的方法是我们可以创建一个可以随时调用的简单函数

<?php
  // Create the Function to get the string
  function GetStringBetween ($string, $start, $finish) {
  $string = " ".$string;
  $position = strpos($string, $start);
  if ($position == 0) return "";
  $position += strlen($start);
  $length = strpos($string, $finish, $position) - $position;
  return substr($string, $position, $length);
  }
?>

有关更多详细信息,请阅读。

另一个非常简单的方法是,我们可以创建一个可以随时调用的简单函数

<?php
  // Create the Function to get the string
  function GetStringBetween ($string, $start, $finish) {
  $string = " ".$string;
  $position = strpos($string, $start);
  if ($position == 0) return "";
  $position += strlen($start);
  $length = strpos($string, $finish, $position) - $position;
  return substr($string, $position, $length);
  }
?>

有关更多详细信息,请阅读。

您应该使用XML。这会让一切变得更简单。你应该使用XML。这会让一切变得更容易。我只有一个问题,乞讨中的是什么,最后是什么?@iUngi,PHP允许您使用几个不同的字符来转义除/以外的正则表达式。我使用它的目的是,如果我必须按字面意思使用它们,我就不必用\/来转义。我只有一个问题,begging中的the是什么,最后的s是什么?@iUngi,PHP允许您使用几个不同的字符来转义除/以外的正则表达式。我使用它是为了,如果我必须按字面意思使用它们,我就不必逃避。