2014-11-01 70 views
0

我试图在php中的字符串中的某个单词之后移除单词。查找字符串并替换单词并删除后续单词PHP

我用这个来代替:

$text = "Example University School of Science"; 
$oldUni = "University"; 
$newUni = "University%"; 
$text = str_replace($oldUni , $newUni , $text); 

我想有只是先“大学”的话,它可以是单词的一个未定义的数字。所以上面看起来像'Example University%'。

我试过str_replace,但只是取代了发生。

回答

0
$text = "Example University School of Science"; 

$var="University"; 

$pos=stripos($text,$var); 


$text_new=substr($text,0,$pos+strlen($var)); 

echo $text_new."%"; 
+0

感谢您为我工作。 – 2014-11-01 23:17:57

1

试试这个:

$array = explode ($text, "University"); 
$newText = $array[0]; 

希望帮助:)

2

阿布您可以使用nevermind的代码,或者如果你正在使用PHP> = 5.3.0下面的代码可用于: -

$text = "Example of the University School of Science"; 
$oldUni = "University"; 

$user = strstr($text, $oldUni, true); 
echo $user . $oldUni .'%'; // prints name 

虽然功能strstr没有存在之前,第三P- arameter(真)在PHP中只加5.3.0

参考: - http://php.net/manual/en/function.strstr.php

相关问题