globex Posted March 2, 2007 Share Posted March 2, 2007 Hello, I have a variable which holds two numbers separated by an 'x'. Like this: 100x100, or 80x34 or 480x12. How do I: a) strip the first number and the x to get just the second number b) strip the x and the last number to get just the first number I'm very new at PHP so some details and simple explanations would be fantastic. Or maybe just point me in the direction of the proper documentation page. Thanks. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 substr www.php.net Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 <?php $num="100x100"; $result=substr($num,0,3); echo "$result<br>"; $num="100x100"; $result=substr($num,4,7); echo "<br>$result"; ?> Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 2, 2007 Share Posted March 2, 2007 <?php $num = 480x12 $numbers = explode("x",$num); echo "First Number: ".$numbers[0]; echo "<br />"; echo "Second Number: ".$numbers[1]; ?> Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 2, 2007 Share Posted March 2, 2007 Yup, I'd use explode too Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 both do the same, the both code examples are as equel as each over and are set as the same speed for a php string function. Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 2, 2007 Share Posted March 2, 2007 Except that using substr wont parse his 80x34 example. Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Share Posted March 2, 2007 does substr() not need a static length? as in that example will it assume its always, for example, 123x456 ie. 3 numbers, then x, then 3 numbers? So it wouldn't work for 4 numbers, or 2 numbers I would also use explode personally. Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 2, 2007 Share Posted March 2, 2007 Amen to Chris. If you used substr with strpos you could find the position of the X and use that, but you would still have to call it twice one to get the beginning and one for the end whereas explode does it in one line and I believe is a simpler way to do it. Quote Link to comment Share on other sites More sharing options...
globex Posted March 3, 2007 Author Share Posted March 3, 2007 Thanks a lot. Explode works like a charm. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.