Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Explode at the last occurance of n
Old 02-27-2009, 04:34 PM Explode at the last occurance of n
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
Was wondering if anyone knows how to split a string into an array at the last occurrence of a repeating number and the number itself if it does not repeat.
ie.
$string = '1978497748773434793';
Needs to become array:
0=>197
1=>84977
2=>4877
3=>34347
4=>93

failed attempts:
explode('7'..
str_split('7'..
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
 
Register now for full access!
Old 02-27-2009, 08:13 PM Re: Explode at the last occurance of n
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I have not tested this code but it should work

PHP Code:
$string '1978497748773434793';
$x 7;
$result = array();
$len strlen($string);

for(
$pos1 $pos2 $pos2 $len$pos2++) {
   if (
$string{$pos2} == $x) {
      
$pos2++;
      while(
$pos2 $len && $string{$pos2} == $x) {
         
$pos2++;
      }
      
// $pos2 should now hold the position of the digit
      // after the last ocurance of $x in this segment
      
$result[] = substr($string$pos1, ($pos2-$pos1));
      
$pos1 $pos2;
   } elseif(
$pos2 == $len-1) {
      
// last digit, add to array evan if it's not $x
      
$result[] = substr($string$pos1$len-$pos1);
   }


Last edited by lizciz; 02-27-2009 at 08:15 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-28-2009, 05:29 AM Re: Explode at the last occurance of n
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Ok, now it's tested, and it works
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-28-2009, 09:31 PM Re: Explode at the last occurance of n
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
wow you rock. Thank you so much for taking the time to do that for me. I racked my brain for a while on that one.
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
Reply     « Reply to Explode at the last occurance of n
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12782 seconds with 12 queries