calande Posted July 28, 2007 Share Posted July 28, 2007 I'm new on this forum, and I'm very happy to join you, it seems there's a lot of people around here. I love PHP/MySQL I'm writing a small application that receives the name and version of an application as parameter, with no space in its name, ie: Firefox2.0.0.5 K3B1.5 OpenOffice.org2.2 What I have to do is extract only the name from this variable, so basically I have to remove the version on the right hand side. What I did is: <?php $pbi="K3B14.5.4"; print eregi_replace('[0-9]*$', '', str_replace ('.', '', $pbi)); ?> Which returns "K3B" so it's working fine, but I'd like to know how you would have done it, just to see other ways of doing it. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
effigy Posted July 30, 2007 Share Posted July 30, 2007 There's no need to replace the period, it can be included in the pattern. I've also used PREG because it's the norm for me since it's more powerful. <pre> <?php $pbi = "K3B14.5.4"; echo preg_replace('/[\d.]*\z/', '', $pbi); ?> </pre> Quote Link to comment Share on other sites More sharing options...
calande Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks! Quote Link to comment Share on other sites More sharing options...
calande Posted July 31, 2007 Author Share Posted July 31, 2007 By the way, do you know where I could find some documentation to understand the above regexp, beside php.net? Quote Link to comment Share on other sites More sharing options...
effigy Posted July 31, 2007 Share Posted July 31, 2007 Check the Resources page. I like regular-expressions.info. The \d is a shorthand for [0-9], a digit. The \z anchors the pattern to the end of the string. Quote Link to comment Share on other sites More sharing options...
calande Posted July 31, 2007 Author Share Posted July 31, 2007 Great, perfect, thank you 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.