PHP:剪切字符串中的所有序列,直到点为止

问题描述 投票:0回答:1

我在PHP 5.6中有一个字符串,其中包含需要提取到新字符串中的序列。但是,由于我是php的新手,所以我们在代码方面遇到困难。

示例:($ searchstring)

“我们在这里有一个示例,涵盖了全部内容的99%,这句话已经包含了一部分。因为这是第一个标记词:还有很多其他内容,例如逗号,特殊字符ä,ü,ß或%等这个适当的Markerword:包含更多次且包含多个骚动字符。如果Markerword:包含此词,则切掉整个序列-直到该Markerword之后的第一个点,以及中间还有其他句子甚至单词或在最后或之前,我们都将其全部忽略。“

序列在两者之间的某个位置,可能只有1次这样的序列,也可能是多次,例如2次,3次或5次...

序列本身总是具有可变的长度和不同的单词/数字。但是它以相同的模式开始和结束:开始:“ 标记词:

”结束:“ ”(“标记字:”之后的第一个点)在需要提取的序列之间,没有句号。

我有一个代码,但是它仅从字符串中提取一个序列(最后一个)。但是,如果还有更多,它们将被跳过/不使用。

我拥有的代码无法100%正常工作:

    $resultstring = false;
if (strpos($searchstring, "Markerword:") !== false){
        preg_match('/(Markerword:([^.]+))/', $searchstring, $matches);
            $resultstring= $matches[0];
            $stopPos = strpos($resultstring, "  ");
            if ($stopPos !== false) {
            $resultstring= substr($resultstring,0,$stopPos + 1);
                }
            }

我如何才能将他们全部都这样淘汰?

以上示例的预期结果:

标记字:还有很多其他内容,例如逗号,特殊字符ä,ü,ß或%等。标记字:多次包含,且以乱序形式包含。标记字:包含此标记,然后切掉整个序列-直到该标记字之后的第一个点。”

我在PHP 5.6中有一个字符串,其中包含需要提取到新字符串中的序列。但是,由于我是php的新手,所以我们为代码苦苦挣扎。示例:($ searchstring)“我们在这里有一个示例,其中...

php string preg-replace
1个回答
0
投票
$searchstring = "We got an example here which covers it to 99% all and this sentence is already part of it. Because this is the first Markerword: with a lot of other things like commata, special characters ä, ü, ß or % and more in it. This proper Markerword: contains more of it multiple times and in caotic characters. If the Markerword: contains this, then cut the whole sequence out - until the first dot after that markerword. And if there are other sentences or even just words inbetween or at the end or before we ignore them all.";

preg_match_all('/\bMarkerword:[^.]+\./', $searchstring, $m);
$result = implode(' ', $m[0]);
echo $result;
© www.soinside.com 2019 - 2024. All rights reserved.