Jump to content

[PHP] String Manipulation


Recommended Posts

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* :P

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 =P

oh.. 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 :D

Edited by Bi0haZarD
Link to comment
Share on other sites


I don't think this is exactly what you need, but it might be usefull.

Fruit.php

<?php
if(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

no sorry doesn't help, where i'm getting this string is from the $_SERVER predefined variable, seen here http://us2.php.net/reserved.variables

but 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

if i'm reading his code right, thats for giving the user a link like mysite.com/pears.php?pears=10&carrots=5

which 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 by Bi0haZarD
Link to comment
Share on other sites

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 by Bi0haZarD
Link to comment
Share on other sites

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
//pears
if(isset($_GET['pears']) && is_numeric($_GET['pears']))
{
echo "Number of Pears = ".$_GET['pears']."<br />";
}
else
{
$_GET['pears'] = 0;
}
//carrots
if(isset($_GET['carrots']) && is_numeric($_GET['carrots']))
{
echo "Number of Carrots = ".$_GET['carrots']."<br />";
}
else
{
$_GET['carrots'] = 0;
}
//oranges
if(isset($_GET['oranges']) && is_numeric($_GET['oranges']))
{
echo "Number of Oranges = ".$_GET['oranges']."<br />";
}
else
{
$_GET['oranges'] = 0;
}
//output
echo $_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 5

I know that, but I meant how to get the variables behind a filename(?xxx) behind another file?

Edited by Martijn
Link to comment
Share on other sites

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 by ColdFusion200
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...