PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Nov 21, 2018 1:58 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Apr 16, 2011 6:19 pm 
Offline
Forum Newbie

Joined: Tue Nov 17, 2009 12:07 pm
Posts: 2
two part question...

PART 1...
part one is really simple, but for some reason it's just not happening for me... i need to find and replace all numbers which have a space between them...

ex: 9 0 8would become 908 & 6 9 would become 69

what're the proper preg_replace attributes i'd need to achieve this?


PART 2...
this one is a little tricky... i also have some numbers like 1,000 that are showing up as 1 ,000 or 1, 000... so i need to get rid of the unwanted space, but there's a catch... if the line which contains the number we're potentially replacing also contains the string respective, then i need leave the space intact. the reason being, some number/comma combinations represent two separate values as opposed to a thousand delimiter (this case can be identified by the line containing somewhere the string respective).

thanks so much


Top
 Profile  
 
PostPosted: Sun Apr 17, 2011 7:50 pm 
Offline
Forum Contributor
User avatar

Joined: Sun Jul 05, 2009 10:39 pm
Posts: 214
Location: SLC, UT
This should do the trick:

Syntax: [ Download ] [ Hide ]
<?php
function fixnums($text) {
        // First remove any one sapce between two digits.
        $text = preg_replace('/(\d) (?=\d)/', '$1', $text);
        // Next process lines which do NOT have the word "respective"
        $re = '/^((?!\brespective\b).)+$/mi';
        $text = preg_replace_callback($re, '_fixnums_callback', $text);
        return $text;
}
function _fixnums_callback($matches) {
        // Remove space from comma separated digits.
        $re = '/(\d)(?: ,|, )(?=\d{3}\b)/';
        return preg_replace($re, '$1,', $matches[0]);
}
?>


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 4 guests


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