PHP:删除字符串中的所有序列直到点

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


例子:($searchstring) “我们这里有一个例子,它覆盖了 99%,这句话已经是其中的一部分了。因为这是第一个标记词:还有很多其他的东西,比如逗号、特殊字符 ä、ü、 ß 或 % 以及更多内容。这个正确的标记词:多次包含它的更多内容,并且是杂乱无章的字符。如果标记词:包含这个,则将整个序列剪掉 - 直到该标记词之后的第一个点。如果还有其他中间、结尾或之前的句子,甚至只是单词,我们都会忽略它们。”


序列介于两者之间,并且可能只有 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);

                }

            }

我怎样才能把他们都这样?


上述示例的预期结果: Markerword:其中包含许多其他内容,例如逗号、特殊字符 ä、ü、ß 或 % 等等。标记词:多次包含更多内容,并且使用粗体字。标记词:包含这个,然后将整个序列剪掉 - 直到该标记词之后的第一个点。”


蝴蝶不菲
浏览 59回答 1
1回答

慕慕森

$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;输出:Markerword: with a lot of other things like commata, special characters ä, ü, ß or % and more in it. Markerword: contains more of it multiple times and in caotic characters. Markerword: contains this, then cut the whole sequence out - until the first dot after that markerword.正则表达式解释
打开App,查看更多内容
随时随地看视频慕课网APP