![](/img/trans.png)
[英]PHP preg_replace find a word in string and remove other characters
[英]Find String and Replace word and remove subsequent words PHP
我正在嘗試刪除php字符串中某個單詞后的單詞。
我正在用它來代替:
$text = "Example University School of Science";
$oldUni = "University";
$newUni = "University%";
$text = str_replace($oldUni , $newUni , $text);
我只想有“大學”之前的單詞,並且可以是未定義數量的單詞。 因此,上面的內容類似於“ Example University%”。
我已經嘗試過str_replace,但這只是替換了出現的情況。
試試這個:
$array = explode ($text, "University");
$newText = $array[0];
希望有幫助:)
$text = "Example University School of Science";
$var="University";
$pos=stripos($text,$var);
$text_new=substr($text,0,$pos+strlen($var));
echo $text_new."%";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.