PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 24, 2018 9:09 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Apr 07, 2012 9:25 am 
Offline
Forum Newbie

Joined: Sat Apr 07, 2012 9:16 am
Posts: 1
Hi all,

I have been lurking on these forums for a while and today I am making my first post as this regex stuff has really been driving me crazy :banghead: !

I need a regex expression that will determine the last number before the decimal point (.) and remove it.

Example: I have a list of file names in a directory such as:
12345678.1
12345.1

By removing the last digit before the 1 I expect these results:
12345678.1 = 1234567.1
12345.1 = 1234.1

I have tried ([A-Z^\.]{1,3}). but it is giving me errors.

Any ideas? Thank you in advance!!

pezkingrich


Top
 Profile  
 
PostPosted: Sat Apr 07, 2012 9:48 pm 
Offline
DevNet Resident
User avatar

Joined: Wed Apr 01, 2009 1:31 pm
Posts: 1532
Syntax: [ Download ] [ Hide ]
$str = preg_replace('/\d\./', '.', $str, 1);

or
Syntax: [ Download ] [ Hide ]
$p = strpos($str, '.');
if ($p > 0 && ctype_digit(substr($str, $p - 1, 1))) {
    $str = substr($str, 0, $p - 1).substr($str, $p);
}

Both solutions do the following transformations:
Syntax: [ Download ] [ Hide ]
'12345678.1' => '1234567.1',
'12345.1' => '1234.1'
'.1' => '.1'
'1.' => '.'
'.' => '.'
'1' => '1'
'1a.' => '1a.'
'1.1.1' => '.1.1'
'' => ''


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group