php preg_split() 查找两个单词之间的文本

发布于 2024-10-17 15:07:03 字数 1334 浏览 1 评论 0原文

如何使用 preg_split() 获取 [item:1] 和 [/item:1] 之间的值
$query= [item:1] 我的内容 [/item:1];

这不起作用
$match = preg_split('/[[item:1][\/item:1]]/', $query);
echo $match[0];

输出应该只是“我的内容”。

更新:

$query = '
[page:1]you got page one content[/page:1]
[page:2]you got page two content[/page:2]
';

// parse function
function parse_page($page, $string) {

    // VERY IMPORTANT STRIP OUT ANYTHING THAT WOULD SCREW UP THE PARSE
    $string = str_replace(array("\r", "\n", "\t", "\f"), array('', ''), $string);

    // test the string with the page number
    preg_match('@\[page:'.$page.'\](.*?)\[/page:'.$page.'\]@', $string, $matches);

    // return the match if it succeeded
    if($matches) {
        return $matches;
    }else {
        return false;
    }
}

// if page is set then try and obtain it
if(isset($_GET['p'])) {

    // set the returned parse value to a variable
    $page_dump = parse_page(($_GET['p']), $query);

    // check to see if the match was successful
    if($page_dump[1]) {
        echo $page_dump[1];
    }else {
        echo '0';
    }
}else {

    // parse page 1, if no page specified.
    $page_dump = parse_page('1', $query);

    if($page_dump[1]) {
        echo $page_dump[1];
    }else {
        echo 'no page =(';
    }
}

how do I use preg_split() to grab the value between [item:1] and [/item:1]
$query= [item:1] my content [/item:1];

This isn't working
$match = preg_split('/[[item:1][\/item:1]]/', $query);
echo $match[0];

output should be just " my content ".

UPDATE:


$query = '
[page:1]you got page one content[/page:1]
[page:2]you got page two content[/page:2]
';

// parse function
function parse_page($page, $string) {

    // VERY IMPORTANT STRIP OUT ANYTHING THAT WOULD SCREW UP THE PARSE
    $string = str_replace(array("\r", "\n", "\t", "\f"), array('', ''), $string);

    // test the string with the page number
    preg_match('@\[page:'.$page.'\](.*?)\[/page:'.$page.'\]@', $string, $matches);

    // return the match if it succeeded
    if($matches) {
        return $matches;
    }else {
        return false;
    }
}

// if page is set then try and obtain it
if(isset($_GET['p'])) {

    // set the returned parse value to a variable
    $page_dump = parse_page(($_GET['p']), $query);

    // check to see if the match was successful
    if($page_dump[1]) {
        echo $page_dump[1];
    }else {
        echo '0';
    }
}else {

    // parse page 1, if no page specified.
    $page_dump = parse_page('1', $query);

    if($page_dump[1]) {
        echo $page_dump[1];
    }else {
        echo 'no page =(';
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-10-24 15:07:03

您不想拆分字符串,而是想捕获模式。为此,请使用 preg_match,例如

if (preg_match('@\[item:1\](.*?)\[/item:1\]@s', $query, $matches)) {
    echo $matches[1];
}

编辑:已测试且工作

编辑2:添加了“s”(PCRE_DOTALL) 修饰符 - http://www.php.net/manual/en/reference.pcre.pattern.modifiers .php

您确实应该考虑更好的标记解决方案(请参阅 XML)

You don't want to split the string, you want to capture a pattern. For that, use preg_match, eg

if (preg_match('@\[item:1\](.*?)\[/item:1\]@s', $query, $matches)) {
    echo $matches[1];
}

Edit: Tested and working

Edit2: Added "s" (PCRE_DOTALL) modifier - http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php.

You should really consider a better markup solution (see XML)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文