簡體   English   中英

從每行中刪除字符后的所有內容,並返回剩余內容

[英]Remove everything after a character from each line and return what's left

假設我有以下文字:

This is line number 1. This is my line number 2. This is his line number 3. This is her line number 4. This is their line number 5.

我想刪除單詞number及其后的所有內容,然后返回剩下的內容(當然,在單獨的行上)。

我希望這是可能的,並且我並沒有使問題變得過於復雜,謝謝!

為您量身打造strstr()以及explode

$string = 'This is line number 1.
This is my line number 2.
This is his line number 3.
This is her line number 4.
This is their line number 5.';

$ex = explode('.',$string);
var_dump($ex);

foreach ($ex as $x) {
    echo strstr($x, 'number',true)."<br>";
}

您可以只使用strpos來找到單詞number ,然后對文本進行子字符串化(每行都在循環中)

http://php.net/manual/zh/function.strpos.php http://php.net/manual/zh-CN/function.substr.php在圓點后加載文本並在生成的數組上循環。 希望它能對我有所幫助

所以這是工作示例:

$string = "This is line number 1";
$pos = strpos($string, "number");
    if ($pos == false) {
        print "Not found\n";
    } else {
        print "Found!\n";
        print substr($string, $pos);
    }

str_replace(“ number”,“”,$ actualstring)也可以正常工作。

暫無
暫無

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

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