Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Stripping out part of a string with PHP

Stripping out part of a string with PHP
Thread Tools
Junior Member
Join Date: May 2003
Status: Offline
Reply With Quote
Jan 28, 2004, 03:58 PM
 
Hello all,

Say I have the string.. "Hello World, how are you today?"

How would I go about removing everything before the comma (,)? So that I'm left with just " how are you today?"

Any advice would be great. I'm looked on php.net, but the closest thing I could find was trim, which didn't seem to do what I want to accomplish.

Thanks!
Jon Marus
www.jmarus.com - kicks by JM
     
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Jan 28, 2004, 04:45 PM
 
I'm sure there are many different way of doing this, but off the top of my head here is an example of how I would do it:

[php]$string = "Hello World, how are you today?";
$string = substr($string, (strpos($string, ',')+1));[/php]
     
jon31  (op)
Junior Member
Join Date: May 2003
Status: Offline
Reply With Quote
Jan 28, 2004, 05:04 PM
 
Thanks! That works perfectly.
www.jmarus.com - kicks by JM
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jan 28, 2004, 07:46 PM
 
Keep in mind that this method will only work when you only have to replace characters at the start of a string.

A more general solution is to use preg_replace(), and simply replace what you've matched with an empty string. That gets the job done in a more flexible manner. If you know you only need to chop off things at the very head of a string, though, substr() is probably faster.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2