Reputation: 1547
I want to look for a sub-string within a string and if it is found I want to take out the sub-string that was found. ex
I'm looking for the word hello in the string helloworld.
if the sub-string is found I want to take out that sub-string and save the rest that was left over in a variable.
$newVar = 'world';
I have a code that looks for a sub-string in a string:
$originalStr = 'helloworld'
$strToCompare = 'hello';
if (stristr($originalStr, $strToCompare)){
//take out the string that was found and save the rest in a variable.
$newVar = 'world';
}
Upvotes: 1
Views: 32
Reputation: 2075
You can use the strpos
function which is used to find the occurrence of one string inside other:
$originalStr = 'helloworld';
$strToCompare = 'hello';
if (strpos($originalStr, $strToCompare)!== FALSE) {
$newVar = str_replace($strToCompare,'',$originalStr);
}
Note that you need to compare with the !==
operator NOT !=
.
Upvotes: 2