get the character before ":"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gpong
Forum Newbie
Posts: 16
Joined: Sun Aug 06, 2006 8:49 am

get the character before ":"

Post by gpong »

<?
$text="PV-3510 : 3.5”Hard Disk Media playerPV-2510 : 2.5”Hard Disk Media"
?>
i have this string and I want to get the character before ":". What should I do? thank you
Appleton
Forum Newbie
Posts: 3
Joined: Fri Jul 06, 2007 4:52 am

Post by Appleton »

Within that variable you have two ":" occurances?

Do you just want 1 character before the ":" which would be " " or are you looking to pick out the model numbers?

PV-3510
PV-2510
etc
gpong
Forum Newbie
Posts: 16
Joined: Sun Aug 06, 2006 8:49 am

help

Post by gpong »

i want to pick out the model numbers. thank you
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

explode() and trim() may be of use :wink:
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Code: Select all

//explode("string seperator", $stringVar)

$code = explode(":", $text);
$model_number = $code[0];
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

the character or string before the semi colon?

use explode and trim like they said to get a clean array of information that you need.

from this string that you have: PV-3510 : 3.5”Hard Disk Media playerPV-2510 : 2.5, you'll end up with a 3 element single array. the first part of the array (index 0) will be the model number. the second and third will be the name and whatever that 2.5 is. from there if you want just a char then you'll have to use substr to get just a single alpha numberic byte.

enjoy
gpong
Forum Newbie
Posts: 16
Joined: Sun Aug 06, 2006 8:49 am

thank you its worked

Post by gpong »

i get it. thank you very much bro
Post Reply