DavidRoberts60 Posted November 9, 2011 Share Posted November 9, 2011 Hello, Is it possible when a variable is passed through to another page (example something like 728334-sdfnjsdh) just to grab the number bit? In other words, get everything before the - symbol? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/250763-getting-everything-before-the/ Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 if you don't care what characters come before the -, and if they are not always going to be digits, you can use this preg_match $string = "728334-sdfnjsdh"; $pattern = "~(.*)-~"; preg_match($pattern,$string,$matches); print_r($matches); $matches[1] will hold the value that you want. If you expect the characters that come before the - to always be digits, then you can use something like this $string = "728334-sdfnjsdh"; $pattern = "~(\d*)-~"; preg_match($pattern,$string,$matches); print_r($matches); where again, $matches[1] will hold your value Quote Link to comment https://forums.phpfreaks.com/topic/250763-getting-everything-before-the/#findComment-1286546 Share on other sites More sharing options...
DavidRoberts60 Posted November 9, 2011 Author Share Posted November 9, 2011 if you don't care what characters come before the -, and if they are not always going to be digits, you can use this preg_match $string = "728334-sdfnjsdh"; $pattern = "~(.*)-~"; preg_match($pattern,$string,$matches); print_r($matches); $matches[1] will hold the value that you want. If you expect the characters that come before the - to always be digits, then you can use something like this $string = "728334-sdfnjsdh"; $pattern = "~(\d*)-~"; preg_match($pattern,$string,$matches); print_r($matches); where again, $matches[1] will hold your value Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/250763-getting-everything-before-the/#findComment-1286550 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.