Php 如何在定义关键字后获取关键字内容

Php 如何在定义关键字后获取关键字内容,php,Php,在关键字get之后获取txts内容的最佳解决方案是什么 我试过: preg_match('/get; txt:"([^"]*)"/', $string, $output_array); 但是它对txt不起作用:它只获取第一个txt。有点过时,但您可以使用explode获取所需的字符串: $str = 'test (get; txt:"Sample text 1"; txt:"Sample text 2"; txt:"</title>"; something_else;)'; $in

在关键字get之后获取txts内容的最佳解决方案是什么

我试过:

preg_match('/get; txt:"([^"]*)"/', $string, $output_array);

但是它对txt不起作用:它只获取第一个txt。

有点过时,但您可以使用explode获取所需的字符串:

$str = 'test (get; txt:"Sample text 1"; txt:"Sample text 2"; txt:"</title>"; something_else;)';
$input = explode(';', $str);
$output = [];
foreach ($input as $key => $value) {
    $tmp = explode(':', $value);
    if (trim($tmp[0]) == 'txt') {
        $output[] = htmlspecialchars(str_replace('"', '', $tmp[1]));
    }
}
print_r($output);

输出:

Array
(
    [0] => Sample text 1
    [1] => Sample text 2
    [2] => </title>
)

你从哪里弄到这些字符串的?它们看起来几乎像JSON。请定义bestYou有太多未关闭的问题,IMHO。
Array
(
    [0] => Sample text 1
    [1] => Sample text 2
    [2] => </title>
)