Jump to content

Removing string from string


MyCode

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.