MyCode Posted July 14, 2010 Share Posted July 14, 2010 Hello, I am trying to remove a dynamic string from a string and would like to ask you if you could point me to a solution. What I am trying to do is removing everything before the first instance of : (: space) For example: Today only: Free Roses should return: Free Roses Special Deal: Free Hamburgers should return: Free Hamburgers So basically I want to show everything after :[space] Any idea how this would be possible? Thank you for this great forum! Link to comment https://forums.phpfreaks.com/topic/207701-removing-string-from-string/ Share on other sites More sharing options...
Chud37 Posted July 14, 2010 Share Posted July 14, 2010 Its not amazingly clear what your trying to achieve, at first i thought you were banging on about the spaces, but it would seem that your actually talking about the colons (":"). So its not that hard. You could explode: $myVar = "Foo:Bar"; $x = explode(":",$myVar); Would return an array in $x, giving you: $x[0] == "Foo"; $x[1] == "Bar"; So if you wanted to do that you could. If you had more than one colon on there be aware that you'd get alot more arrays in your output. Alternatively you could locate the first instance of the colon, i think using array_search. I havent tried it but array_search returns the key of the given string in an array. The first instance in fact. So, in theory, the code should go something like this: $myVar = "Foo : Bar"; $x=array_search(":",$myVAR); Or you could try $x=array_search(" : ",$myVAR); $x should return 4 or 5, i forget if it will start at 0 or 1. Once you have your located ':' you can then use substr(); to get the rest. I'd rather do this method really it seems quicker. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/207701-removing-string-from-string/#findComment-1085773 Share on other sites More sharing options...
MyCode Posted July 14, 2010 Author Share Posted July 14, 2010 Thank you very much, your solution worked very well and sorry for the confusion about the space, I just tried to post exactly what I was looking for and the space was always a part of the strings I try to manipulate, so I included it Link to comment https://forums.phpfreaks.com/topic/207701-removing-string-from-string/#findComment-1085781 Share on other sites More sharing options...
Fergal Andrews Posted July 14, 2010 Share Posted July 14, 2010 Hi MyCode, You could use this: function createSubstring($string) { $pos = strpos($string, ":"); $substring = substr($string, $pos - 1, strlen($string)); return $substring; } all the best, Fergal Link to comment https://forums.phpfreaks.com/topic/207701-removing-string-from-string/#findComment-1085782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.