ccherry17 Posted February 8, 2012 Share Posted February 8, 2012 Hello again to all I would like to ask if how can i removed the first 3 words on this string? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit accumsan turpis, vitae rutrum quam cursus id. Sed quis pulvinar eros. Integer vel tellus turpis. Donec tortor dolor, convallis in mollis et, rhoncus eu lacus. Etiam quam risus, fringilla ac tempor in, congue vel felis. Mauris eu luctus augue. Fusce nisl neque, convallis a posuere a, ultricies et metus. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/ Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 are these 3 words subject to change? Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315749 Share on other sites More sharing options...
ccherry17 Posted February 8, 2012 Author Share Posted February 8, 2012 No sir.. Those words will be totally removed.. How will i do it? thanks Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315750 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 there are a couple ways, what im asking is, will the first three words always be Loren ipsum dolor or could they be something else like Hello and welcome? Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315751 Share on other sites More sharing options...
ccherry17 Posted February 8, 2012 Author Share Posted February 8, 2012 So sorry sir, yup the three words are subject to change. ex. Hello and welcome mine +440983234213 adam Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315752 Share on other sites More sharing options...
spiderwell Posted February 8, 2012 Share Posted February 8, 2012 why not adapt the answer in your previous thread use explode, use array shift 3 times, then use impode to put back into a string. this might not be the best method but it would work. Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315755 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 You should look at the PHP function explode, here's a little example I made. <?PHP $string = 'mine +440983234213 adam look at this sentence, first 3 words are removed! <br>'; echo $string; $explode_str = explode(' ', $string, 4); $string = $explode_str[3]; echo $string; ?> Try that out and see how it goes, remember to have a little read up on the function too. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315756 Share on other sites More sharing options...
ccherry17 Posted February 8, 2012 Author Share Posted February 8, 2012 Thank you paul! Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315758 Share on other sites More sharing options...
Adam Posted February 8, 2012 Share Posted February 8, 2012 The most efficient way of doing this would be to use a series of strpos calls. The first would need to find the first occurrence of a space from the start of the string; the second would need to find the next occurrence from after the point where the first was found; and then the third call would need to find the next occurrence from after the point where the second was found. Once you know the position of the third space, you can use substr to select the text after that point: $str = 'one two three four'; $pos1 = strpos($str, ' '); $pos2 = strpos($str, ' ', $pos1+1); $pos3 = strpos($str, ' ', $pos2+1); if ($pos3 !== false) { $str = substr($str, $pos3+1); } Using explode() will create a potentially large array when it's not necessary - much faster to seek through the string to find the positions. Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315761 Share on other sites More sharing options...
spiderwell Posted February 8, 2012 Share Posted February 8, 2012 i knew there would be a string position method too, but i went for the less brain powered answer Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315767 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 Nice example Adam, I had a feeling one of those string functions would be better. Quote Link to comment https://forums.phpfreaks.com/topic/256663-how-to-remove-first-3-words-on-a-string/#findComment-1315774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.