denoteone Posted April 9, 2009 Share Posted April 9, 2009 If I had a string that assigned to a variable like. $mystring = "hello welcome to my house. my name is mike I live here"; and I want to strip everything that appears before the word "mike" I looked at the sting functions on http://www.w3schools.com/PHP/php_ref_string.asp but did not see any that would work. I am thinking i need to do this in 2 steps. like explode that string then just get rid of the first object in the array? can anyone help me out with this? Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/ Share on other sites More sharing options...
Axeia Posted April 9, 2009 Share Posted April 9, 2009 Since it sounds like you're going to use it more than once it would be a good idea to make a function that returns everything after the word. <?php //Your Idea function getTextAfter( $strSplitOn, $sentense ) { $arrResults = explode( $str, $sentense ); return $arrResults[1]; } //Another way function getTextAfter( $strSplitOn, $sentense ) { $iPos = strPos( $sentense, $strSplitOn ); return substr( strPos+strlen($strSplitOn), $sentense ); //Need to add the words length to the position it was found at, otherwise $strSplitOn //is in the returned result. } ?> [edit]: btw, don't paste both like that in your code or you'll always be using the bottom one. So either paste one a time, or rename one of them. [2nd edit]: It's actually the second result in the array, as arrays start at [ 0 ] and not at [ 1 ]. [ 0 ] would be the first result, and it's the part you wanted to strip. and as an example on how to use it: echo getTextAfter("mike", $mystring ) Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/#findComment-805729 Share on other sites More sharing options...
denoteone Posted April 9, 2009 Author Share Posted April 9, 2009 I will test the first one. I am wondering though if i am exploding at a certain character does that character still get saved in the array? Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/#findComment-805733 Share on other sites More sharing options...
laffin Posted April 9, 2009 Share Posted April 9, 2009 did u read up on strpos, it finds strings within a string and returns the position. add in strlen, to get the length of the needle word. and substr to remove things ya dun want <?php $keyword="mike"; $mystring = "hello welcome to my house. my name is mike I live here"; $pos=strpos($mystring,$keyword)+strlen($keyword); $newstring=substr($mystring,$pos); echo "'{$newstring}'"; ?> Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/#findComment-805734 Share on other sites More sharing options...
Axeia Posted April 9, 2009 Share Posted April 9, 2009 The thing you're looking for (the first parameter of explode) is lost, it wont be in the array. Of course you could simply use $arr[] = $missingPart; Also note that both of these functions will behave differently if there is more than one instance of $str in $sentense. The first will return the part between the 2 $str occurrences. The last one will return everything after the first occurrence of $str. (if you want everything after the last occurrence use strrpos instead of strpos. Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/#findComment-805740 Share on other sites More sharing options...
denoteone Posted April 9, 2009 Author Share Posted April 9, 2009 The first will return the part between the 2 $str occurrences. This is what i am trying to accomplish So I am using it and it is working great! Thanks Link to comment https://forums.phpfreaks.com/topic/153361-solved-remove-everything-that-appears-before-a-specific-string/#findComment-805742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.