Synapse Posted December 23, 2005 Share Posted December 23, 2005 (edited) Hello, been lookin all over the net on how to do this... but can't find out how..I want to be able to parse out only 2 parts of a string that i'm retrieving from the user.. *This is needed in PHP incase you didn't see the title* take this for example...apples=1&Bananas=5&Carrots=3421&Pears=7&Watermelons=10 now.. the data i want to parse out is Carrots and Pears... discarding the rest of the data.. and i want them to be assigned to 2 variables automatically, $Carrots and $Pears which i can then echo.. I'm a newb when it comes to PHP so bear with me =Poh.. and have $carrots = '3421' not.. $carrots = 'Carrots=3241' etc..Thanks *EDIT*ok, since were on the topic of string manipulation.. and i don't want to have 2 topics going.. how would i be able to check if carrots is greater then 1000 then display something else..example.. if $carrots > '1001' then$carrots = 'Over 1,000 Carrots'thanks again Edited December 23, 2005 by Bi0haZarD Link to comment Share on other sites More sharing options...
Martijn Posted December 23, 2005 Share Posted December 23, 2005 I don't think this is exactly what you need, but it might be usefull.Fruit.php<?phpif(isset($_GET['pears']) && is_numeric($_GET['pears'])){ echo "Number of pears = ".$_GET['pears']."<br />";}if(isset($_GET['carrots']) && is_numeric($_GET['carrots'])){ echo "Number of carrots = ".$_GET['carrots']."<br />";}else{ echo "Error: id is incorrectly or does not excist";}echo $_GET['pears'] + $_GET['carrots']." pieces of vegetables and fruit";?>The last echo outputs the total numbers of the two strings.Usage:fruit.php?pears=(number)&carrots=(number)(number)= a numberical value, the error is displayed when a non-numberical value is supplied.Hope this helps Link to comment Share on other sites More sharing options...
Synapse Posted December 23, 2005 Author Share Posted December 23, 2005 no sorry doesn't help, where i'm getting this string is from the $_SERVER predefined variable, seen here http://us2.php.net/reserved.variablesbut i get a bunch of data that i don't need in 1 string/variable.I guess i should be lookin more towards string trimming and trim the data out of there.. which i hate doing lol. Link to comment Share on other sites More sharing options...
hazard_abio Posted December 23, 2005 Share Posted December 23, 2005 no sorry doesn't help, where i'm getting this string is from the $_SERVER predefined variable, seen here http://us2.php.net/reserved.variablesbut i get a bunch of data that i don't need in 1 string/variable.I guess i should be lookin more towards string trimming and trim the data out of there.. which i hate doing lol.So you are using the 'QUERY_STRING'.The easiest way to do this is by what Martijn said Get or Post querystring. Link to comment Share on other sites More sharing options...
Synapse Posted December 23, 2005 Author Share Posted December 23, 2005 (edited) if i'm reading his code right, thats for giving the user a link like mysite.com/pears.php?pears=10&carrots=5which sure does work, but i don't want to have to do this since the user can easily spoof this by changing it in the URL... insted the data i want is sent much like the GET, User-Agent, Host, Connection, Cookie, etc..so in my code i have something like this..<?$myinfo = $_SERVER['HTTP_MY_INFO'];echo "Hello: $myinfo"?>the problem being is that it comes back with a string like this "Hello: apples=1&Bananas=5&Carrots=3421&Pears=7&Watermelons=10" but all i want out of it would bee the Carrots and the Pears.Hope that made sence. Edited December 23, 2005 by Bi0haZarD Link to comment Share on other sites More sharing options...
ColdFusion200 Posted December 23, 2005 Share Posted December 23, 2005 you'd be best of using GET and then using $_GEThow exactly do you want this string to be sent? and what does it do? Link to comment Share on other sites More sharing options...
Synapse Posted December 23, 2005 Author Share Posted December 23, 2005 (edited) the string is sent when a user clicks a link in my aim profile which opens the aimtoday window and is redirected to my site.., the data that is sent that i want is the users screenname (which is easy with something like profile.php?username=%n.. but since i need the build number also I'd like to not do this..) and the build number... so for example..you click on my link in my profile using aim 5.5.35xx thus opening up a 533 x 360 window with Internet Explorer Embeded... then you can see my profile and i can do things like put movies in there.. pictures, etc... which i'm hoping i can do things like, write a tutorial in there, put up unlimited links, etc.. or anything else useful..what i want to do with it.. is if the build number is between 2 certain numbers to use the 533 x 360 background wallpaper.. if its anything higher, to use a seperate bigger background... thus getting rid of a tiled look..the retrieval of the screenname is just for cosmetics.. to do something like Hello Bi0haZarD, and so on..this is mainly to teach myself PHP.. or i would jsut find an alternative like smartprofile to do this... Edited December 23, 2005 by Bi0haZarD Link to comment Share on other sites More sharing options...
ColdFusion200 Posted December 23, 2005 Share Posted December 23, 2005 use $_GET Link to comment Share on other sites More sharing options...
Martijn Posted December 23, 2005 Share Posted December 23, 2005 but how do you want to get the string behind profile.php after (in my example) fruits.php?profile.php?username=something to fruits.php?username=something I mean Link to comment Share on other sites More sharing options...
ColdFusion200 Posted December 23, 2005 Share Posted December 23, 2005 if you open the url 'this.php?pears=2&apples=5$_GET['pears'] will return 2 and $_GET['apples'] will return 5 Link to comment Share on other sites More sharing options...
Martijn Posted December 23, 2005 Share Posted December 23, 2005 (edited) Cleaned up my file and added oranges, I made it so that it doesn't matter wich variable you provide and in wich order.<html><head><title>Fruits and Vegetables Counter</title></head><body><?php//pearsif(isset($_GET['pears']) && is_numeric($_GET['pears'])){ echo "Number of Pears = ".$_GET['pears']."<br />";}else{ $_GET['pears'] = 0;}//carrotsif(isset($_GET['carrots']) && is_numeric($_GET['carrots'])){ echo "Number of Carrots = ".$_GET['carrots']."<br />";}else{ $_GET['carrots'] = 0;}//orangesif(isset($_GET['oranges']) && is_numeric($_GET['oranges'])){ echo "Number of Oranges = ".$_GET['oranges']."<br />";}else{ $_GET['oranges'] = 0;}//outputecho $_GET['pears'] + $_GET['carrots'] + $_GET['oranges']." pieces of fruits and vegetables";?> </body></html>if you open the url 'this.php?pears=2&apples=5$_GET['pears'] will return 2 and $_GET['apples'] will return 5I know that, but I meant how to get the variables behind a filename(?xxx) behind another file? Edited December 23, 2005 by Martijn Link to comment Share on other sites More sharing options...
ColdFusion200 Posted December 23, 2005 Share Posted December 23, 2005 (edited) how do you mean exactly?the string is sent when a user clicks a link in my aim profile which opens the aimtoday window and is redirected to my site.., the data that is sent that i want is the users screenname (which is easy with something like profile.php?username=%n.. but since i need the build number also I'd like to not do this..) and the build number... so for example..you click on my link in my profile using aim 5.5.35xx thus opening up a 533 x 360 window with Internet Explorer Embeded... then you can see my profile and i can do things like put movies in there.. pictures, etc... which i'm hoping i can do things like, write a tutorial in there, put up unlimited links, etc.. or anything else useful..what i want to do with it.. is if the build number is between 2 certain numbers to use the 533 x 360 background wallpaper.. if its anything higher, to use a seperate bigger background... thus getting rid of a tiled look..the retrieval of the screenname is just for cosmetics.. to do something like Hello Bi0haZarD, and so on..this is mainly to teach myself PHP.. or i would jsut find an alternative like smartprofile to do this...if ($_GET['build'] == 1.2.3) { echo 'this_background.jpg';} elseif ($_GET'build' == 4.5.6) { echo 'that_background.jpg';} else { echo 'another_background.jpg;} Edited December 23, 2005 by ColdFusion200 Link to comment Share on other sites More sharing options...
Synapse Posted December 23, 2005 Author Share Posted December 23, 2005 (edited) *EDIT*solution found on another forum..thanks for the help guys. Edited December 23, 2005 by Bi0haZarD Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now