Jump to content

Recommended Posts

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?

 

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 )

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}'";
?>

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.