On 4/28/07, Micky Hulse <micky@xxxxxxxxxxxxx> wrote:
Hi, I pieced-together a script that will insert a string into another string at a set interval of words... See here: <http://www.ambiguism.com/sandbox/truncate/truncate.php> [Click the "view source" link to view source.] :D Basically, I need a setup a page where my client can paste an article, automate the insertion of a template tag (in this case: {page_break}) and then copy/paste into the blog/cms. My goal for this script was to make it easy for the CMS to create word-balanced pages. Long story short, I got what I think will be an acceptable script... but I would love to get feedback. What do you think? What can I improve? What am I doing wrong? The script does not need to be super-robust, but would love to hear what the PHP gurus have to say. ;) Many thanks in advance! Cheers, Micky
Nice idea, but it seems that it isn't working correctly. I did this example: <?php for($x = 0; $x < 100; $x++) { $string .= " abc"; } echo break_up($string, 10, "||"); ?> With your break_up function. It results this: abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc While i expected this: abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc ||abc abc abc abc abc abc abc abc abc abc And i found where the problem is, it is this line: $num += $num; // Set next number flag to look for. This works only for one time, but after that, it doubles whole time, so in above example, $num would go like this: 10 20 40 80 So to fix, replace this part of the code: for($i=0; $i < count($array); $i++) { # For every string in the array, we make one more test to assure it is a word. # We assume that are words only strings that contain at least a letter character (we will not count commas for example). # Checks for existence of letter characters, including the special characters and increments the number of words if found. if(eregi('[0-9A-Za-zÀ-ÖØ-öø-ÿ]', $array[$i])) { if($words == $num) { $hldr[$up++]; // $num flag met, add one to $hldr array. $num += $num; // Set next number flag to look for. } else { $hldr[$up] .= $array[$i].' '; // $num flag not met yet, insert a space. } $words++; } } with this: $look = $num for($i=0; $i < count($array); $i++) { # For every string in the array, we make one more test to assure it is a word. # We assume that are words only strings that contain at least a letter character (we will not count commas for example). # Checks for existence of letter characters, including the special characters and increments the number of words if found. if(eregi('[0-9A-Za-zÀ-ÖØ-öø-ÿ]', $array[$i])) { if($words == $look) { $hldr[$up++]; // $num flag met, add one to $hldr array. $look += $num; // Set next number flag to look for. } else { $hldr[$up] .= $array[$i].' '; // $num flag not met yet, insert a space. } $words++; } } That's it. Tijnema -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php