Jump to content

Removing version number of an application in a string


calande

Recommended Posts

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!  :)

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.