willpower Posted February 19, 2007 Share Posted February 19, 2007 Hi Basically I want to use a function like str_replace, except that I only want to replace the 5th occurence of the character to be replaced. Why? Well I have a DB with some shoppiung contents in it. If any of you have dealt with PROTX card payments you will know that the basked has to be passed in a single $var with a field delimeter of ":" With this single line I want to output this to a txt doc. I have replaced all ":" with chr(9) (This is the Tab Key) to give me my spacing in the txt doc. But now I need to create a newline (\n) on every 5th chr(9) (formerly ":") Hope that made sense. Any help appreciated. Will Quote Link to comment Share on other sites More sharing options...
skali Posted February 19, 2007 Share Posted February 19, 2007 you can $tempstring = substr($yourstring,strpos($tempstring,":")) your line in a loop and can get the 5th ":" easily. Quote Link to comment Share on other sites More sharing options...
printf Posted February 19, 2007 Share Posted February 19, 2007 Do you want to keep the \t and put \n after it, or replace every fifth \t with \n? Quote Link to comment Share on other sites More sharing options...
willpower Posted February 19, 2007 Author Share Posted February 19, 2007 replace every 5th \t with \n Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted February 19, 2007 Share Posted February 19, 2007 Looks like you need to write your own str_replace... $count = 0; for each $character in $string { if( $character == 9 ) { if( $count == 5 ) { $new .= "\n" $count = 0; } else { $new .= "\t"; $count ++; } } else { $new .= $character; } } return $new; Any help? monk.e.boy Quote Link to comment Share on other sites More sharing options...
willpower Posted February 19, 2007 Author Share Posted February 19, 2007 ...im on it now....i'll let you know in 5 mins Quote Link to comment Share on other sites More sharing options...
effigy Posted February 19, 2007 Share Posted February 19, 2007 <pre> <?php $numbers = range(1, 100); $number_string = join(':', $numbers); function regex_reformat () { static $count = 0; ++$count; return $count % 5 ? "\t" : "\n"; } $new_string = preg_replace_callback('/:/', 'regex_reformat', $number_string); echo $new_string; ?> </pre> Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 I might be a little quacky right now, but why couldn't we just do one regular expression here? $newString=preg_replace("@([^\t]+\t){4}([^\t]+)(\t)@", "$1$2\n", $value); Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 19, 2007 Share Posted February 19, 2007 Just loop through the string for the characters you are looking for and replace the occurrence... $string = "abcdef:ghijkl:mnop:qrs:tuvw:xyz:ABCD:EFG:HIJKL:MNOPQ:RSTU:VW:XYZ"; $num_found = 0; for ($i = 0; $i < strlen($string); $i++) { if ($string{$i} == ":") { $num_found++; if ($num_found == 5) { $string{$i} = "\n"; $num_found = 0; } } } echo $string; Quote Link to comment Share on other sites More sharing options...
printf Posted February 19, 2007 Share Posted February 19, 2007 <?php $str = '1 2 3 4 5 6 7 8 9 10 11'; $str = preg_replace ( "/((.+?\t){4})(.+?)\t/", "\\1\\3\n", $str ); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted February 19, 2007 Share Posted February 19, 2007 <?php $numbers = range(1, 100); $number_string = join(':', $numbers); function regex_reformat () { static $count = 0; ++$count; return $count % 5 ? "\t" : "\n"; } $new_string = preg_replace_callback('/:/', 'regex_reformat', $number_string); echo $new_string; ?> That's quite nice monk.e.boy Quote Link to comment Share on other sites More sharing options...
willpower Posted February 19, 2007 Author Share Posted February 19, 2007 Effigy I refer to your soloution I just get the numbers 1 to 100 ouputed thru the echo. May be my fault as I have to say that I dont wholly understand your code. This is what I have $output="Item1:25:5:--:30:Item2:50:10:00:60:Item3:25:5:--:30"; $numbers = range(1, 100); $output = join(':', $numbers); function regex_reformat () { static $count = 0; ++$count; return $count % 5 ? "\t" : "\n"; } $new_string = preg_replace_callback('/:/', 'regex_reformat', $output); echo $new_string; Where am I going wrong? Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 Remove these two lines: $numbers = range(1, 100); $output = join(':', $numbers); Quote Link to comment Share on other sites More sharing options...
willpower Posted February 19, 2007 Author Share Posted February 19, 2007 Well Ladies and Gentlemen...it may not have been a smooth path...but I'm there. Thanks to all who helped. As always PHP Freaks is the best Will Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.