Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何取出字符串的特定部分并将剩余字符串保存在变量中?_Php_String - Fatal编程技术网

Php 如何取出字符串的特定部分并将剩余字符串保存在变量中?

Php 如何取出字符串的特定部分并将剩余字符串保存在变量中?,php,string,Php,String,我想在字符串中查找子字符串,如果找到了,我想取出找到的子字符串。ex 我在helloworld字符串中查找hello这个词。 如果找到子字符串,我想取出该子字符串,并将剩下的保存在变量中。 $newVar='world' 我有一个在字符串中查找子字符串的代码: $originalStr = 'helloworld' $strToCompare = 'hello'; if (stristr($originalStr, $strToCompare)){ //take out the string

我想在字符串中查找子字符串,如果找到了,我想取出找到的子字符串。ex
我在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);
}
注意您需要与
进行比较==操作员
=