The Bat Posted December 2, 2008 Share Posted December 2, 2008 Hi everyone, my PHP skills are a bit rusty, so... I'm using basename($_SERVER['PHP_SELF']) to get (for example) blahblah_blah_stuff.php. What would be the best way to extract just the word "stuff" (the word right before ".php" and right after the last underscore) from it? Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/ Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 Here is a regular expression you can use: <?php $pattern = '/([^_]+)\.[\w]+$/'; $subject = basename($_SERVER['PHP_SELF']); preg_match($pattern, $subject, $matches); print_r($matches); ?> Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703569 Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 This goofy solution might work too... <?php $file = basename($_SERVER['PHP_SELF']); $file = substr(strrpos($file, '_') + 1, strrpos($file, '.') + 1); ?> Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703571 Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 Nevermind that last one doesn't work. lol. Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703575 Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 But this works! I wouldn't use it though.... <?php $file = 'blahblah_blah_stuff.php'; $file = substr($file, strrpos($file, '_') + 1, strrpos($file, '.')- strrpos($file, '_') - 1 ); echo $file; ?> Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703577 Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 Here is a function that you could use, it is a little more robust. Covers several cases: <?php function foo($string) { $parts = explode('_', $string); if (count($parts) > 0) { $end = end($parts); if (($pos = strrpos($end, '.')) !== false) { return substr($end, 0, $pos); } else { return $end; } } return $string; } ?> Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703582 Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 <?php $string = "blahblah_blah_stuff.php"; $postfix = $postfix[count($postfix = preg_split('/_|\./',$string))-2]; echo $postfix; ?> Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703600 Share on other sites More sharing options...
The Bat Posted December 2, 2008 Author Share Posted December 2, 2008 <?php $string = "blahblah_blah_stuff.php"; $postfix = $postfix[count($postfix = preg_split('/_|\./',$string))-2]; echo $postfix; ?> Thanks for the help everyone, I'll go with Crayon's solution. Link to comment https://forums.phpfreaks.com/topic/135078-solved-quick-string-question/#findComment-703609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.