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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/ Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 substr www.php.net Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197849 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"; ?> Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197851 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]; ?> Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197899 Share on other sites More sharing options...
mbtaylor Posted March 2, 2007 Share Posted March 2, 2007 Yup, I'd use explode too Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197900 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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197904 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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197912 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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197913 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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-197973 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. Link to comment https://forums.phpfreaks.com/topic/40862-stripping-characters-from-a-variable/#findComment-198366 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.