Stuart_Westgate Posted February 4, 2013 Share Posted February 4, 2013 This is an example of my code: $status = $entry->title; echo substr(strstr($status," "), 1) . substr($status, 0, -18); Below is the outcome: I'm feeling cold in this room. #emotionalclothingPants: I'm feeling cold in this room. I want the oucome to look like this: I'm feeling cold in this room. Thus removing the "#emotionalclothing" part of the string How can this be achieved? Thanks Stuart Quote Link to comment https://forums.phpfreaks.com/topic/274008-how-can-i-edit-the-same-string-twice-and-only-get-one-output-string/ Share on other sites More sharing options...
Barand Posted February 4, 2013 Share Posted February 4, 2013 (edited) $status = str_replace('#emotionalclothing','',$status); Edited February 4, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274008-how-can-i-edit-the-same-string-twice-and-only-get-one-output-string/#findComment-1410009 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 (edited) You have to store the result of the first operation in the variable, before you attempt the second operation. What you're doing right now is: First you remove everything before the first spacestrstr($status," ") Then you cut that down to the first character only (the space).substr (...., 1) Next you cut the last 18 characters off the original string.substr ($status, 0, -18); Before you concatenate (glue) the two results together, and echo them out.echo ***** . *****; Also, from what I can see, you only need to edit the string once, using one function. As Barand just posted. No need to make it more complicated than it have to be, after all. Edited February 4, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/274008-how-can-i-edit-the-same-string-twice-and-only-get-one-output-string/#findComment-1410010 Share on other sites More sharing options...
Stuart_Westgate Posted February 4, 2013 Author Share Posted February 4, 2013 Thanks for your answers guys. It's appreciated. I've been programming for a few years not and in my final year of uni (still have a lot to learn though lol) but only just started doing PHP. I got a little caught up in the code and just didn't think to simplify it lol. This is how the string would be formated everytime before it's parsed. So you have item of clothing: Pants: Emotion: I'm feeling cold in this room. Hashtag: #emotionalclothing Pants: I'm feeling cold in this room. #emotionalclothing I done it this way in the end: $status = $entry->title; $status1 = substr(strstr($status," "), 1); $status2 = substr($status1, 0, -18); echo $status2; Which outputs: I'm feeling cold in this room. It's always good to give something back! Quote Link to comment https://forums.phpfreaks.com/topic/274008-how-can-i-edit-the-same-string-twice-and-only-get-one-output-string/#findComment-1410018 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.