[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 08-10-2005, 09:01 PM   #1 (permalink)
NamePros Member
 
Join Date: Jan 2005
Location: Texas USA
Posts: 71
203.00 NP$ (Donate)

Outer is an unknown quantity at this point


Alternating string splits

I need to know how to break a string up into alternating sizes.

for example, I need this string broken up: "hi, how are you, peace!"
so results are like this:
"hi"
", h"
"ow"
" ar"
" y"
"ou "
", p"
"eac"
"e!"

Note the spaces. Basically, I need a string to be cut up by a pattern of 2 characters, then 3 characters, then 2 characters, then three characters

How would I do that in PHP?

Thank You
__________________
I wonder...
Outer is offline  
Old 08-11-2005, 03:05 AM   #2 (permalink)
NamePros Member
 
Icespadez's Avatar
 
Join Date: Apr 2005
Location: Boston, MA
Posts: 26
290.00 NP$ (Donate)

Icespadez is an unknown quantity at this point


Try this (I think you forgot the 'e' in 'are' in your example =P)

PHP Code:

    $string
= 'hi, how are you, peace!';

    function
customSplitString($text) {
        
$text_pieces = array();
        
$offset = 0;
        
$piece_length = 2;
        
$str_length = strlen($text);
        
        for (
$x=0; $offset < $str_length; $x++) {
            
$piece_length = ($x % 2) ? 3 : 2;
            
$text_pieces[] = substr($text, $offset, $piece_length);
            
$offset += $piece_length;
        }
        return
$text_pieces;
    }

    
print_r(customSplitString($string));
Icespadez is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking php function to strip invalid characters from string RJ Programming 9 03-31-2005 11:34 PM
XML return string namescanada Programming 0 02-16-2005 02:11 PM
PHP Help Needed (String Manipulation) 100 NB Awarded DomainSpring Programming 5 09-05-2004 12:46 AM
String parsing prob, special characters web guru Programming 2 10-03-2003 11:50 AM
VeriSign splits off domain registrar. Jeff Industry News 0 06-17-2003 07:59 PM

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:22 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85