Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 Preg在同一文件中匹配几次 文本1文本2 一些文字-很长,不有趣。 正文3正文4_Php_Regex_Preg Match - Fatal编程技术网

Php Preg在同一文件中匹配几次 文本1文本2 一些文字-很长,不有趣。 正文3正文4

Php Preg在同一文件中匹配几次 文本1文本2 一些文字-很长,不有趣。 正文3正文4,php,regex,preg-match,Php,Regex,Preg Match,在使用preg match函数时,如何一次处理文本1、文本2、文本3和文本4?$pattern='([^请尝试: <b>Text 1</b><br><i>Text 2</i> Some text - very long and not interesting. <b>Text 3</b><br><i>Text 4</i> $pattern = '<b>([^&

在使用preg match函数时,如何一次处理文本1、文本2、文本3和文本4?

$pattern='([^请尝试:

<b>Text 1</b><br><i>Text 2</i>

Some text - very long and not interesting.

<b>Text 3</b><br><i>Text 4</i>
$pattern = '<b>([^<]+)</b><br><i>([^<]+)</i>';
preg_match_all($pattern, $string, $matches);

不清楚为什么您既不想使用
DOM
,也不想使用
preg\u match\u all
,但是使用
preg\u match
会像:

array (size=4)
  0 => string 'Text 1' (length=6)
  1 => string 'Text 2' (length=6)
  2 => string 'Text 3' (length=6)
  3 => string 'Text 4' (length=6)
$s='YOUR INPUT';
$pos=0;
$res=[];
while(preg_match(“/(.*?)/m',$s,$vals,preg_OFFSET_CAPTURE,$pos))
{
$pos=$vals[1][1];
$res[]=$vals[1][0];
}
印刷品(港币);;

您能详细介绍一下您要做的事情吗?使用DOM解析器来处理HTML,而不是regexp。
array (size=4)
  0 => string 'Text 1' (length=6)
  1 => string 'Text 2' (length=6)
  2 => string 'Text 3' (length=6)
  3 => string 'Text 4' (length=6)
$s = 'YOUR INPUT';

$pos = 0;
$res = [];
while(preg_match('/<[bi]>(.*?)<\/[bi]>/m', $s, $vals, PREG_OFFSET_CAPTURE, $pos))
{
  $pos = $vals[1][1];
  $res[] = $vals[1][0];
}

print_r($res);