godsent Posted December 30, 2009 Share Posted December 30, 2009 for example I have string [<<][<<][<<][<<], how could I make that it will be shown as [<<]4, because it repeats 4 times. I have no clue about this, please help me if you can. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 30, 2009 Share Posted December 30, 2009 Can you provide more information? Are all the strings going to have the exact same pattern "[<<]"? Or can they use different patterns, e.g. "[<>][<>][<>][<>]" If the paterns will always be "[<<]" then you can simply use preg_match(). If the patterns can vary, but will be in a fixed "list" of patterns then you can use the same logic. However, if the patterns will vary with no known list to compare against it will be much more difficult as you will probably need to build a looping function to try and determin the pattern. Quote Link to comment Share on other sites More sharing options...
godsent Posted December 31, 2009 Author Share Posted December 31, 2009 Yes you are right that text not always going to be [<<], might be any for example [A][A][A][A][C][C][C] => [A]4[C]3 Quote Link to comment Share on other sites More sharing options...
salathe Posted December 31, 2009 Share Posted December 31, 2009 One quick way would be to use a regex replacement; I've no idea of your familiarity with regular expressions so the following might go over your head and be totally inappropriate (no point using code which is gobbledygook!). (Try this code) $subject = '[A][A][A][A][C][C][C]'; function callback($match) { return $match[1] . (strlen($match[0]) / strlen($match[1])); } echo preg_replace_callback('/(\[[^\]]+\])\1+/', 'callback', $subject); 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.