亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP:刪除字符串中的所有序列直到點

PHP:刪除字符串中的所有序列直到點

PHP
蝴蝶不菲 2023-04-23 17:50:11
我在 PHP 5.6 中有一個字符串,其中包含我需要提取到新字符串中的序列。但是由于我是 php 的新手,所以我們很難處理代碼。例子:($searchstring) “我們這里有一個例子,它覆蓋了 99%,這句話已經是其中的一部分了。因為這是第一個標記詞:還有很多其他的東西,比如逗號、特殊字符 ?、ü、 ? 或 % 以及更多內容。這個正確的標記詞:多次包含它的更多內容,并且是雜亂無章的字符。如果標記詞:包含這個,則將整個序列剪掉 - 直到該標記詞之后的第一個點。如果還有其他中間、結尾或之前的句子,甚至只是單詞,我們都會忽略它們?!毙蛄薪橛趦烧咧g,并且可能只有 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:其中包含許多其他內容,例如逗號、特殊字符 ?、ü、? 或 % 等等。標記詞:多次包含更多內容,并且使用粗體字。標記詞:包含這個,然后將整個序列剪掉 - 直到該標記詞之后的第一個點?!?
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

$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.

正則表達式解釋


查看完整回答
反對 回復 2023-04-23
  • 1 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號