Hey, this is a question that is for my news posting script.

I want to take a string such as "[private]this will not be seen[/private]" and remove that whole string from an entry. Now the text inside the [private] tags will of couse be different words and lengths.

I want tp have it set up so that people with lower-level access will not be able to read the text inside the [private][/private] tags. Any help on how to do this?

    Regular Expressions are the way to go for that kind of stuff...

    [man]preg_match[/man] perhaps... I am not 100% though, as I don't like or deal with RegExp very well... There are a lot of regs here though that are good with RegExp.

      Someone more knowlegeable than may have a better way to do this, but one simple way would be to have assign a session value based on membership level to the users, and then use preg_replace to replace the 'private' text with some default text (or just a blank space) for those junior members.

      HTH,

        Thanks, is anyone able to provide me with a small example?
        I am really having a hard time with this and I am not that good with regular expressions. I'm trying to learn them.

          $str = "blah [private]blabber mouth[/private] blah";
          $str = eregi_replace('\[private\][.]*\[/private\]','',$str);
          

          output is blah blah

          Though I didn't test this and I can't remember off the top of my head if you'll need to place a \ before the /

            Here's a (non-regex) recursive function which takes the input string by reference. It'll work with any tag as long as the closing tag is the same as the opening tag except for the "/":

            function strip_tagged_part(&$str, $tag1)
            {
                $tag2 = substr($tag1, 0, 1) . '/' . substr($tag1, 1);
                $len2 = strlen($tag2);
                $pos1 = strpos($str, $tag1);
                $pos2 = strpos($str, $tag2);
                if (($pos1 !== false) && ($pos2 !== false)) {
                    $str = substr($str, 0, $pos1) . substr($str, $pos2 + $len2);
                    strip_tagged_part($str, $tag1);
                }
            }
            
            $msg = '[private]blah[/private]String to be [private]blah[/private]stripped.';
            $tag = '[private]';
            strip_tagged_part($msg, $tag);
            echo $msg;

            EDIT:
            Or, if you want to save the removed parts in an array:🆒

            function strip_tagged_part(&$str, &$prvt, $tag1)
            {
                $tag2 = substr($tag1, 0, 1) . '/' . substr($tag1, 1);
                $len1 = strlen($tag1);
                $len2 = strlen($tag2);
                $pos1 = strpos($str, $tag1);
                $pos2 = strpos($str, $tag2);
                if (($pos1 !== false) && ($pos2 !== false)) {
                    $prvt[] = substr($str, $pos1 + $len1, $pos2 - $pos1 - $len1);
                    $str = substr($str, 0, $pos1) . substr($str, $pos2 + $len2);
                    strip_tagged_part($str, $prvt, $tag1);
                }
            }
            
            $msg = '[private]blah[/private]String to be [private]blah[/private]stripped.';
            $tag = '[private]';
            strip_tagged_part($msg, $tagged_part, $tag);
            echo $msg . '<br />';
            foreach ($tagged_part as $value) {
                echo $value . '<br />';
            }

              Nice, Installer. Using a string function will likely save overhead on small/medium chunks of text as well.

                Write a Reply...