Php 如何取出字符串的特定部分并将剩余字符串保存在变量中?
我想在字符串中查找子字符串,如果找到了,我想取出找到的子字符串。exPhp 如何取出字符串的特定部分并将剩余字符串保存在变量中?,php,string,Php,String,我想在字符串中查找子字符串,如果找到了,我想取出找到的子字符串。ex 我在helloworld字符串中查找hello这个词。 如果找到子字符串,我想取出该子字符串,并将剩下的保存在变量中。 $newVar='world' 我有一个在字符串中查找子字符串的代码: $originalStr = 'helloworld' $strToCompare = 'hello'; if (stristr($originalStr, $strToCompare)){ //take out the string
我在helloworld字符串中查找hello这个词。
如果找到子字符串,我想取出该子字符串,并将剩下的保存在变量中。
$newVar='world'代码>
我有一个在字符串中查找子字符串的代码:
$originalStr = 'helloworld'
$strToCompare = 'hello';
if (stristr($originalStr, $strToCompare)){
//take out the string that was found and save the rest in a variable.
$newVar = 'world';
}
您可以使用strpos
函数,该函数用于查找一个字符串在另一个字符串中的出现情况:
$originalStr = 'helloworld';
$strToCompare = 'hello';
if (strpos($originalStr, $strToCompare)!== FALSE) {
$newVar = str_replace($strToCompare,'',$originalStr);
}
注意您需要与进行比较==代码>操作员不=代码>