簡體   English   中英

刪除模式開始的字符串的結尾PHP

[英]Remove the end of a string where a pattern begins PHP

我正在檢索一個字符串,我想刪除單詞“ read more”之后的其余字符串。

例如

$ str ='關於某事的長篇大論,請在其他網站上閱讀有關某事的更多信息';

我不想使用substr,因為數據的長度可能會有所不同。

有任何想法嗎?

您可以組合strpos()substr() ,如下所示:

$firtpart = substr($str, 0, strpos($str,'read more'));

或使用explode()將字符串分成兩部分,並采用第一部分:

list($firtpart) = explode('read more', $str);

甚至使用正則表達式:

$firtpart = preg_replace("/read more.*$/", '', $str);

strstr($str, 'read more', true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM