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! Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/ 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> Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/#findComment-310877 Share on other sites More sharing options...
calande Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/#findComment-311307 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? Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/#findComment-311543 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. Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/#findComment-311814 Share on other sites More sharing options...
calande Posted July 31, 2007 Author Share Posted July 31, 2007 Great, perfect, thank you Link to comment https://forums.phpfreaks.com/topic/62212-removing-version-number-of-an-application-in-a-string/#findComment-311818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.