PHP Preg Match,获取特定模式之间的值

时间:2016-01-15 04:52:20

标签: php regex preg-match

我有一个php应用程序,它将以下输出保存在数据库中:

::cck_gx::gx::/cck_gx::
::i1|0|gx::Lorem::/i1|0|gx::
::head1|0|gx::ipsum::/head1|0|gx::
::tx1|0|gx::dolor, fithos lusec.::/tx1|0|gx::
::cckend_gx::::/cckend_gx::
::cck_gx::gx::/cck_gx::
::i1|1|gx::calendar::/i1|1|gx::
::head1|1|gx::1 Fatura Grátis Por Ano::/head1|1|gx::
::tx1|1|gx::10% de cada mensalidade é reservado, e o valor acumulado até a renovação do Seguro Porto Seguro ou Azul Seguros, é devolvido em forma de desconto. Ou seja, Cliente Conecta pode ter uma fatura de celular grátis por ano.::/tx1|1|gx::

我想使用preg_match从此输出中检索信息。例如,在下面的例子中检索“Lorem”和“ipsum”的相同位置的任何值:

::i1|0|gx::Lorem::/i1|0|gx::
::head1|0|gx::ipsum::/head1|0|gx::

但我对preg_match语法一无所知。

我知道我需要为每个“标签”使用不同的preg匹配(比如preg_match用于检索所有“i1”值,不同的preg_match用于检索所有“head1”等等)。我只需要一个能够理解正确模式的例子。

另外,在最后一行是一个包含许多不同字符的例子,如数字,逗号,“%”和其他字符,我不确定这是否会混淆语法。

这是我失败的两次尝试:

preg_match('~[::i1|0|gx::](.*?)[/::i1|0|gx::]~', $maindata->introtext, $match1a);
 preg_match('::i1|0|gx::(.*?)::/i1|0|gx::', $maindata->introtext, $match1a);
 preg_match('/::i1|0|gx::(.*?)::.i1|0|gx::/', $maindata->introtext, $match1a);

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助

<?php
    $str = '::i1|0|gx::Lorem::/i1|0|gx::';
    preg_match('/(?<=gx::).*(?=::\/)/', $str);

您也可以使用preg_match_all()

<?php
    $str = '::cck_gx::gx::/cck_gx::
    ::i1|0|gx::Lorem::/i1|0|gx::
    ::head1|0|gx::ipsum::/head1|0|gx::
    ::tx1|0|gx::dolor, fithos lusec.::/tx1|0|gx::
    ::cckend_gx::::/cckend_gx::
    ::cck_gx::gx::/cck_gx::
    ::i1|1|gx::calendar::/i1|1|gx::
    ::head1|1|gx::1 Fatura Grátis Por Ano::/head1|1|gx::';

    preg_match_all('/(?<=gx::).*(?=::\/)/', $str, $matches);
    var_dump($matches);

(?<=gx::)正面观察 - 断言下面的正则表达式可以匹配

.匹配任何字符(换行符除外)

*在零和无限次之间,尽可能多次

(?=::\/)肯定前瞻 - 断言下面的正则表达式可以匹配

::匹配字符::字面

\/匹配字符/字面

答案 1 :(得分:0)

您可以提出以下正则表达式:

::(\w+)[^::]+::(?<content>.*?)::(?=\/\1)

PHP代码片段以及freespacing模式下正则表达式的说明如下所示。请参阅example for it on regex101

<?php
$string = '
::cck_gx::gx::/cck_gx::
::i1|0|gx::Lorem::/i1|0|gx::
::head1|0|gx::ipsum::/head1|0|gx::
::tx1|0|gx::dolor, fithos lusec.::/tx1|0|gx::
::cckend_gx::::/cckend_gx::
::cck_gx::gx::/cck_gx::
::i1|1|gx::calendar::/i1|1|gx::
::head1|1|gx::1 Fatura Grátis Por Ano::/head1|1|gx::
';

$regex = '~
        ::
        (\w+)
        # tag 
        [^:]+::
        # match everything except a colon, then two colons 
        (?<content>.*?)
        # match everything lazily and capture it in a group called content
        ::
        # two colons 
        (?=\/\1)
        # closing tag with tag captured in group 1
        ~x';
preg_match_all($regex, $string, $matches);
print_r($matches["content"]);
/* output:
Array
(
    [0] => gx
    [1] => Lorem
    [2] => ipsum
    [3] => dolor, fithos lusec.
    [4] => 
    [5] => gx
    [6] => calendar
    [7] => 1 Fatura Grátis Por Ano
)
*/
?>