We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33992 ☆ A M B ☆
    • 455 Posts
    Hi,

    I need to find a word and put it between two <span> tags. I did it with str_ireplace but it will miss the original case of word. for example if the word is "PHP" and there is "php" in text, "PHP" will replaced with "php"!

    I decided to use regex functions. The best sample of this work was "MODx search highlight plugin", I got the regex codes from it but it does not find any thing!!!
    <?php
    $pcreModifier = ($modx->db->config['charset'] == 'utf8') ? 'iu' : 'i';
    $pattern = '/' . preg_quote($_POST['highlight'], '/') . '(?=[^>]*<)/' . $pcreModifier;
    $replacement = '<span class="highlight">${0}</span>';
    $rc = preg_replace($pattern, $replacement, $rc);
    ?>


    I used the pattern with preg_mach too and it returned 0 while the word exists.

    what is wrong in this regex? it works well in  search highlight plugin.
      God loves me. 【ツ】


      MODX.ir (Persian Support)

      Boplo.ir/modx/ (Persian)
      • 26903
      • 1,336 Posts
      Can you not use str_replace, from the PHP manual :-
      Note: This function is case-sensitive. Use str_ireplace() for case-insensitive replace.
        Use MODx, or the cat gets it!
        • 16034
        • 107 Posts
        AHHP: I don’t see why the regex shouldn’t work, could you dump some debug info (the contents of $pattern, $replacement and $rc before preg_replace?

        shamblett:
        AHHP wants to replace php regardless of case with <span class="highlight">php</span> with the same case.

        str_replace would require you to write all "cases" of php:
        PHP, PHp, PhP, Php, pHP, pHp, phP and php
        (for a N letter word there are 2^N combinations...)

          MODx snippet-glossary 101:
          Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
          • 20413
          • 2,877 Posts
          In my ajaxsearch config I have this:

          if (!function_exists(’stripOtherTags’))
          {
           function stripOtherTags($text){
           // Regular expressions to remove +something+
           $modRegExArray[] = ’~\+(.*?)\+~’;   // +phx+
           // Regular expressions to remove some bad words
           $badWords = array(’pagetitle’,’longtitle’,’getfield’,’GetField’,’Getfield’,’getField’);        // any bad words you want
           foreach($badWords as $bw) $modRegExArray[] = ’~’.$bw.’~’;  
           
           // Remove modx sensitive tags
           foreach ($modRegExArray as $mReg)$text = preg_replace($mReg,’’,$text);
           return $text;
           }
          }

          Is there any way I can get this case insensitive? Ive tried playing with:  preg_replace($mReg,’’,$text); to add a /i  :P


          SORRY FOR CHANGING TOPIC. Just answer this anyone seeing it obvious...
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 10487 MODX Staff
            • 1,535 Posts
            @mrhaw: This should suffice:
            $text = preg_replace($mReg.'i','',$text);

            The regex is being surrounded by tildes (~) and not forward slashes - so you don’t need to include the forward slash (and the tildes are added in the loop for the $badWords array)
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 20413
              • 2,877 Posts
              Sweeet! Thank you!! cool grin
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                • 33992 ☆ A M B ☆
                • 455 Posts
                Quote from: Kleist at Dec 10, 2008, 05:37 AM

                AHHP: I don’t see why the regex shouldn’t work, could you dump some debug info (the contents of $pattern, $replacement and $rc before preg_replace?

                The $rc is simple text and input is a word in it like AHHP word in this post!

                Infact, this codes worked well but after some changes on script it do not work now. I guessed that perhaps there is problem with arriving $_POST data or $rc to this codes so i changed preg with simple str_replace to check it. word replaced well. it shows that input values are correct and preg does not change the $rc!!!

                do you have any suggestion to get same result? replace with same case?
                  God loves me. 【ツ】


                  MODX.ir (Persian Support)

                  Boplo.ir/modx/ (Persian)
                  • 16034
                  • 107 Posts
                  I suggest that you show the output I suggested. I’m sure there is something wrong in one of the variables if it doesn’t work. ;-)
                    MODx snippet-glossary 101:
                    Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
                    • 3749
                    • 24,544 Posts
                    Quote from: AHHP at Dec 10, 2008, 03:11 AM

                    Hi,

                    I need to find a word and put it between two <span> tags. I did it with str_ireplace but it will miss the original case of word. for example if the word is "PHP" and there is "php" in text, "PHP" will replaced with "php"!

                    If you’re sure there’s only one replacement to be made, this will do it (pc is for "preserve case"):

                    $rc = pc_add_tag($_POST['highlight'],$rc,'<span class="highlight">','</span>');
                    
                    return $rc;
                    
                    
                    function pc_add_tag($findMe, $text, $beforeTag, $afterTag) {
                    
                    /* The long version, so you can see what it's doing. 
                    $pos = stripos($text,$findMe);
                    $len = strlen($findMe);
                    $original = substr($text, $pos, $len);
                    $rText = $beforeTag . $original . $afterTag ;
                    return str_replace($original,$rText,$text);
                    */
                    return(str_replace(substr($text, stripos($text,$findMe), strlen($findMe)),$beforeTag . substr($text, stripos($text,$findMe), strlen($findMe)) . $afterTag, $text));
                    
                    }


                    This requires PHP5 (but could be modified for PHP4). If you might have more than one word to highlight in the selection, it gets more complicated.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 33992 ☆ A M B ☆
                      • 455 Posts
                      Thank you Bob,
                      I test your code, i created a loop to replace more than one word, i was working on it and i found a PHP function for highlighting.

                      Someone gave me this: http://aidanlister.com/repos/v/function.str_highlight.php
                      This a useful function by Aidan Lister <[email protected]>

                      I got these codes from str_highlight and it works  grin :
                      $pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#i';
                      $needle_s = preg_quote($_POST['highlight']);
                      $regex = sprintf($pattern, $needle_s);
                      $r['content'] = preg_replace($regex, '<span class="highlight">\1</span>', $r['content']);
                      


                      Thank you all
                        God loves me. 【ツ】


                        MODX.ir (Persian Support)

                        Boplo.ir/modx/ (Persian)