Orionsbelter Posted December 20, 2012 Share Posted December 20, 2012 I'm currently struggling to how i can do the following for example if i have a file name such as 2-lined-noted-pads-565.php and i need the last 3 digits the ID number of the product to be saved into a variable; how can i go about this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/ Share on other sites More sharing options...
requinix Posted December 20, 2012 Share Posted December 20, 2012 If you actually have a file named "2-lines-noted-pads-565.php" then just hardcode the number. Then kick yourself for actually having a file named that. Hopefully you have URL rewriting. If that's the case then your rewriting should match that number and pass it to the underlying script. Doing so depends on (cough) what you have right now... Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/#findComment-1400511 Share on other sites More sharing options...
codefossa Posted December 20, 2012 Share Posted December 20, 2012 $str = "2-lined-noted-pads-565.php"; $id = preg_replace('/(.*?)-([0-9]*)\.php$/', '$2', $str); echo $id; // Prints: 565 Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/#findComment-1400513 Share on other sites More sharing options...
requinix Posted December 20, 2012 Share Posted December 20, 2012 ...Of course, if the question has nothing to do with URL rewriting and more about having the name of a file in a string... Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/#findComment-1400557 Share on other sites More sharing options...
codefossa Posted December 20, 2012 Share Posted December 20, 2012 ...Of course, if the question has nothing to do with URL rewriting and more about having the name of a file in a string... I would assume. He didn't really explain very much, so I just guessed. Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/#findComment-1400611 Share on other sites More sharing options...
requinix Posted December 20, 2012 Share Posted December 20, 2012 He didn't really explain very much, so I just guessed. Ditto. Quote Link to comment https://forums.phpfreaks.com/topic/272203-php-file-name-strip-for-id-code/#findComment-1400636 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.