Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Select specific numbers from a string
Old 10-06-2009, 11:05 AM Select specific numbers from a string
WebTraffic's Avatar
Extreme Talker

Posts: 225
Name: Brandon
Trades: 0
How do I select specific numbers from a string?

I am trying to pass a value along to my php form handler to take the field and select the 4th and 5th number out of the string. The purpose for this is for a person to type in their model number and based on the two numbers it will tell them what size of a unit they have.

Example: YAZA36VX1W

Say this is a model number... I need to be able to tell what the bolded numbers are in order to do what I am wanting to do with this form. I'm thinking it is going to have something to do with the strip function but I don't know.

Brandon
__________________

Please login or register to view this content. Registration is FREE

Coding is just like a woman. If you don't do something exactly right, it complains.
WebTraffic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-06-2009, 01:49 PM Re: Select specific numbers from a string
WebTraffic's Avatar
Extreme Talker

Posts: 225
Name: Brandon
Trades: 0
would I need to use count() somehow in this? I don't really know where to start.
__________________

Please login or register to view this content. Registration is FREE

Coding is just like a woman. If you don't do something exactly right, it complains.
WebTraffic is offline
Reply With Quote
View Public Profile
 
Old 10-06-2009, 02:08 PM Re: Select specific numbers from a string
WebTraffic's Avatar
Extreme Talker

Posts: 225
Name: Brandon
Trades: 0
Actually scratch this. I talked to my boss and the number that I'm looking for isn't necessarily in the same place every time. I need to find a string inside of a string.

I need to look for the numbers 24, 30, 36, 42, 48, 60

PHP Code:
if (how do i calculate this == '24') {
echo 
"You have a 2 ton system.";
} else if (
how do i calculate this == '30') {
echo 
"You have a 2.5 ton system.";
} else if (
how do i calculate this == '36') {
echo 
"You have a 3 ton system.";
} else if (
how do i calculate this == '42') {
echo 
"You have a 3.5 ton system.";
} else if (
how do i calcualte this == '46') {
echo 
"You have a 4 ton system.";
} else if (
how do i calculate this == '60') {
echo 
"You have a 5 ton system.";

This is kinda what I want to do.. only I don't know how to determine if a string contains a certain value.

Example:
Y2436A15
Y24A1536
Y3624A15

No matter where the number is, it will recognize that it is part of the string.
__________________

Please login or register to view this content. Registration is FREE

Coding is just like a woman. If you don't do something exactly right, it complains.
WebTraffic is offline
Reply With Quote
View Public Profile
 
Old 10-06-2009, 02:58 PM Re: Select specific numbers from a string
NullPointer's Avatar
Will Code for Food

Posts: 2,786
Name: Matt
Location: Irvine, CA
Trades: 0
I think strstr() is what you're looking for.

http://us.php.net/manual/en/function.strstr.php

Edit:

I forgot to mention: this is one of the few cases in PHP where datatype is important. Make sure you use strings as input or you might get some unexpected behavior:
PHP Code:
strstr($subject'42'); //correct
strstr($subject42); //incorrect 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-06-2009 at 03:00 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-06-2009, 04:47 PM Re: Select specific numbers from a string
WebTraffic's Avatar
Extreme Talker

Posts: 225
Name: Brandon
Trades: 0
Thank you, this helped me out a lot.
__________________

Please login or register to view this content. Registration is FREE

Coding is just like a woman. If you don't do something exactly right, it complains.
WebTraffic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Select specific numbers from a string
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46819 seconds with 12 queries