使用PHP在每两个特定字符之间获取字符串

时间:2018-12-11 09:51:39

标签: php

我有这样的字符串:

The time is over. # its mean I'm need to die. Please help me. # Ghost. I am here alone. Sorry. # help yourself.

我想获取每个#和点(。)之间的文本 所以我用这个:

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

$parsed = trim(get_string_between($url, '#', '.'));
echo $parsed;

问题在于该函数仅返回第一个示例与我的查询匹配。我不知道要像我选择的那样对每个查询执行此操作。

在此示例字符串中,需要返回以下内容:

its mean I'm need to die

Ghost

help yourself

编辑@Nick答案

我真正的字符串是这样:

Text Text Text # Very good. #:* after this come example. Text Text Text #Very good number 2.

您的代码还向我返回了#:*之后的字符串,我只需要看下面这些示例即可: # Text Text. #Text Text.

在给定的示例中,我需要获取以下文本:

Very goodVery good number 2

2 个答案:

答案 0 :(得分:4)

更新

基于OP的修改,需要更改正则表达式,以便在#之后立即对空格或字母字符使用正向超前查找,即

/#(?=[ A-Za-z])\s*([^.]*)\./

要使用修改后的文本,请执行以下操作:

$string = "Text Text Text # Very good. #:* after this come example. Text Text Text #Very good number 2.";
preg_match_all('/#(?=[ A-Za-z])\s*([^.]*)\./', $string, $matches);
print_r($matches[1]);

输出

Array
(
    [0] => Very good
    [1] => Very good number 2
)

更新了demo on rextester

原始答案

您可以使用preg_match_all获得所需的结果。此正则表达式在#.之间寻找一组字符,通过使用非贪婪捕获组和捕获组任一侧的\s*来去除两端的空白:

$string = "The time is over. # its mean I'm need to die .
Please help me. # Ghost. I am here alone.
Sorry. # help yourself.";
preg_match_all('/#\s*([^.]*?)\s*\./', $string, $matches);
print_r($matches[1]);

输出:

Array
(
    [0] => its mean I'm need to die
    [1] => Ghost
    [2] => help yourself
)

Demo on rextester

答案 1 :(得分:0)

explodesubstrstrpos的组合可以做到:

#分割字符串,然后使用#.获得substr和第一个strpos之间的字符串。

<?php

$examples = [
    'The time is over. # its mean I\'m need to die.',
'Please help me. # Ghost. I am here alone.',
'Sorry. # help yourself.'];

foreach($examples as $example) {
    $exploded = explode('#', $example);
    $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.')));
    var_dump($substr);
}

在一个用于特定字符串的函数中:

$test = parseString('Sorry. # help yourself.');
function parseString($string) {
    $exploded = explode('#', $string);
    $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.')));

    return $substr;
}

var_dump($test);

对于字符串输入,我们必须做一个额外的步骤,在此之前将\n中断:

$stringExample = "The time is over. # its mean I'm need to die.
Please help me. # Ghost. I am here alone.
Sorry. # help yourself.";


$test2 = parseString2($stringExample);
function parseString2($string) {
    $result = [];
    $array = explode("\n", $string);

    foreach($array as $a) {
        $exploded = explode('#', $a);
        $substr = trim(substr($exploded[1], 0, strpos($exploded[1], '.')));    
        $result[] = $substr;
    }

    return $result;
}
var_dump($test2);

对于没有换行符的字符串输入,一个小解析器可能看起来像:

$stringExample2 = "The time is over. # its mean I'm need to die. Please help me. # Ghost. I am here alone. Sorry. # help yourself.";


var_dump(parseString3($stringExample2));
function parseString3($stringExample)
{
    $result2 = [];

    $startBlock = false;

    $block = 0;
    foreach (str_split($stringExample) as $char) {
        if ($char === '#') { // Start block
            $startBlock = true;
        } else if ($startBlock && $char === '.') { // End block
            $result2[$block] = trim($result2[$block]); // Remove unnecessary whitespace
            $block++;
            $startBlock = false;
        } else if ($startBlock) { // Character to append to block
            if (!isset($result2[$block])) { // We have to check if the block has been started already and if not, create it as an empty string because otherwise we would get a notice when trying to append our character to it.
                $result2[$block] = '';
            }
            $result2[$block] .= $char;
        }

    }
    return $result2;
}

如果您使用这些代码中的任何一个,请确保真正了解正在发生的事情并使用足够的变量名,这些只是小的示例代码片段。

所有示例及其输出都可以在下面的3v4l链接中找到

https://3v4l.org/k3TXM