Current location: Hot Scripts Forums » Programming Languages » PHP » How to cut off all the Characters Before a Certain Character


How to cut off all the Characters Before a Certain Character

Reply
  #1 (permalink)  
Old 01-16-06, 12:28 PM
cebuy cebuy is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
How to cut off all the Characters Before a Certain Character

OK I have a variable that often has a colon ":" in the value.
Example: $state_city = "North Carolina : Durham"

If I want to name another variable $city and only want to get the characters after ":", how do I do that?

Result would be $city = "Durham"

Also want to do some kind of check as well for the presence of the : and there is also the possibility of more than one colon, and all I want is the characters after the last colon. Example if it said with 2 colons

$city_state= "USA:North Caolinaurham"
I still want to it to be $city = "Durham"

If it is does not have a colon, I would like for it to just to take the entire text....
$state_city = "Durham"
and give reult as
$city = "Durham"

I know there must be a combination of an "if else" loop and a certain way to grab only the characters to the right of the last colon, but my newbie php skills are not up to task yet. Any help from a master of the craft would be great appreciated! Thanks!
__________________
Thanks!
Chris Chandler
WebMaster of:
Loan Quote, Christian eBuy, & North Alabama,
Reply With Quote
  #2 (permalink)  
Old 01-16-06, 12:53 PM
Larry Kubin Larry Kubin is offline
New Member
 
Join Date: Jan 2006
Location: Austin, TX
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Use the strpos() function to determine the position of the ':' in the string. Then use the substr() function to get a substring starting from the character after that position to the end of the string.

I believe this is what you are trying to do:

Code:
$state_city = 'North Carolina : Durham';
$position = strpos($state_city, ':'); 
$city = substr($state_city, $position);
To test if the ':' exists, you can use strpos(). It returns false if the character you are searching for does not exist in the string. Now that I think about it, since you want to test for multiple colon characters, you should strrpos() instead. It will find the position of the last occurrence of the character you are searching for, and false if the character is not found. So this will probably do what you want:

Code:
if($position = strrpos($state_city, ':')) {
  $city = substr($state_city, $position);
}
else {
  $city = $state_city;
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanted to replace special characters in MS Access using VB, but more than 1 character cebuy Visual Basic 3 12-04-05 05:30 PM
Replace any character ronlar PHP 2 09-22-05 12:46 PM
How to replace extended Character with character luv_suresh Perl 1 06-22-05 12:16 AM
Filtering extended ASCII characters xgab Perl 4 05-23-04 08:13 AM
(O/T: Regex) Finding a series of non-space characters Sargant PHP 3 04-04-04 01:46 PM


All times are GMT -5. The time now is 06:28 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.