Jump to content

simplifying part of the text


godsent

Recommended Posts

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.

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.