Get value before and/or after "x" in a string?

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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Get value before and/or after "x" in a string?

Post by dominod »

Hi,

I am working with screen resolutions and so far I managed to get the users screen resolution into a cookie which states "1440x900" (My resolution).

I just wonder how I can get width and height in two different strings? Something that removes "x" and everything after it to get width and vice verca for height..

:drunk:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Get value before and/or after "x" in a string?

Post by Christopher »

Code: Select all

list($width, $height) = explode('x', $resolution);
(#10850)
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Get value before and/or after "x" in a string?

Post by dominod »

That was easy! lol

Thanks alot! :)

Have a nice one!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Get value before and/or after "x" in a string?

Post by s.dot »

You should use the example above.

I was just bored and did it a different way.

Code: Select all

$str = '1440x900';
$pos = strpos($str, 'x');
$width = substr($str, 0, $pos);
$height = substr($str, $pos+1); 
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Get value before and/or after "x" in a string?

Post by BDKR »

s.dot wrote: You should use the example above.

I was just bored and did it a different way.

Code: Select all

$str = '1440x900';
$pos = strpos($str, 'x');
$width = substr($str, 0, $pos);
$height = substr($str, $pos+1); 
You are right that he should use the example posted above, but it's good you posted this example as not all strings are formatted so clearly or nicely as to facilitate the use of explode.
Post Reply