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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.