【问题标题】:How can I get string between two characters [string] ? PHP [duplicate]如何获取两个字符 [string] 之间的字符串? PHP [重复]
【发布时间】:2012-06-12 12:34:16
【问题描述】:
$string1 = "This is test [example]";
$string2 = "This is test [example][2]";
$string3 = "This [is] test [example][3]";

我怎样才能得到以下结果?

For $string1 -> example
For $string2 -> example*2
For $string3 -> is*example*3

【问题讨论】:

  • 在任何语言中,如果遇到[ 设置标志并抓取所有内容,直到] 并取消设置标志:)

标签: php string


【解决方案1】:
preg_match_all('/\[([^\]]+)\]/', $str, $matches);

php > preg_match_all('/\[([^\]]+)\]/', 'This [is] test [example][3]', $matches);
php > print_r($matches);
Array
(
    [0] => Array
        (
            [0] => [is]
            [1] => [example]
            [2] => [3]
        )

    [1] => Array
        (
            [0] => is
            [1] => example
            [2] => 3
        )

)

这里是正则表达式的解释:

\[ # literal [
( # group start
    [^\]]+ # one or more non-] characters
) # group end
\] # literal ]

【讨论】:

  • 你能解释一下正则表达式吗?据我所知。 / 开始正则表达式。 ` to escape [. [` 包括字符集?正确的 ?你能解释一下完整的正则表达式吗?
【解决方案2】:

对于那些对正则表达式持谨慎态度的人,这里有一个没有疯狂正则表达式语法的解决方案。 :-) 它曾经让我非常恼火,因为这样的东西不是 PHP 的字符串函数所固有的,所以我构建了一个......

// Grabs the text between two identifying substrings in a string. If $Echo, it will output verbose feedback.
function BetweenString($InputString, $StartStr, $EndStr=0, $StartLoc=0, $Echo=0) {
    if (!is_string($InputString)) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. \$InputString is not a string.</p>\n"; } return; }
    if (($StartLoc = strpos($InputString, $StartStr, $StartLoc)) === false) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. Could not find \$StartStr '{$StartStr}' within \$InputString |{$InputString}| starting from \$StartLoc ({$StartLoc}).</p>\n"; } return; }
    $StartLoc += strlen($StartStr);
    if (!$EndStr) { $EndStr = $StartStr; }
    if (!$EndLoc = strpos($InputString, $EndStr, $StartLoc)) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. Could not find \$EndStr '{$EndStr}' within \$InputString |{$InputString}| starting from \$StartLoc ({$StartLoc}).</p>\n"; } return; }
    $BetweenString = substr($InputString, $StartLoc, ($EndLoc-$StartLoc));
    if ($Echo) { echo "<p>html_tools.php BetweenString() Returning |'{$BetweenString}'| as found between \$StartLoc ({$StartLoc}) and \$EndLoc ({$EndLoc}).</p>\n"; }
    return $BetweenString; 
}

当然,这可以压缩很多。为了节省其他人清理它的工作:

// Grabs the text between two identifying substrings in a string.
function BetweenStr($InputString, $StartStr, $EndStr=0, $StartLoc=0) {
    if (($StartLoc = strpos($InputString, $StartStr, $StartLoc)) === false) { return; }
    $StartLoc += strlen($StartStr);
    if (!$EndStr) { $EndStr = $StartStr; }
    if (!$EndLoc = strpos($InputString, $EndStr, $StartLoc)) { return; }
    return substr($InputString, $StartLoc, ($EndLoc-$StartLoc));
}

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多