We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6511
    • 60 Posts
    danny_kay1710 Reply #51, 16 years ago
    As a side note if you don’t want your keywords to be case sensitive replace str_replace in the code with str_ireplace
      • 3071
      • 99 Posts
      No, not actualyl the menu. I have some text in my left div so it picks that up. I went into my template and added <!-- Begin Content --> before my main content div, but it still picks the text up in that left div for some reason.
        • 6511
        • 60 Posts
        danny_kay1710 Reply #53, 16 years ago
        If it’s in the same place as the main content then it will at the moment.

        Writing a workaround.
          • 6511
          • 60 Posts
          danny_kay1710 Reply #54, 16 years ago
          <?php
          $output = $modx->documentOutput;
            
          $rStart = strpos($output, '<!-- Keyword Replacement Start -->');
          $rStop =  strpos($output, '<!-- Keyword Replacement End -->');
          
          $rLength = $rStop - $rStart;  
            
          if (($rStop !== false) & ($rStart !== false))
          {
              
              if (!function_exists('str_replace_once')) {
                  function str_replace_once($needle , $replace , $haystack){
                      // Looks for the first occurence of $needle in $haystack
                      // and replaces it with $replace.
                      $pos = strpos($haystack, $needle);
                      if ($pos === false) {
                          // Nothing found
                          return $haystack;
                      }
                      return substr_replace($haystack, $replace, $pos, strlen($needle));
                  }
              }
          
          
              $replacementText = ' <a href="machinery-safety">machinery safety</a>';
          
              $subStr = substr($output, $rStart, $rLength);
              $rSubStr = $subStr ;
                
              if (strpos($subStr, $replacementText ) === false)
                $rSubStr = str_replace_once(' machinery safety', $replacementText, $subStr);    
                
                
              $modx->documentOutput = str_replace($subStr, $rSubStr, $output);
          }
          ?>​
          

          OK in this version put <!-- Keyword Replacement Start --> before the content you wish to run the replace on and <!-- Keyword Replacement End --> afterwards. You can put it in as many places as you want.
            • 3071
            • 99 Posts
            [s]I tried this and it just doesn’t pick up the start and end points at all? Example of my start point:

             </div> <!-- Keyword Replacement Start -->
                <div id="center">
            
            [/s]

            It seems to be working now, thank you very much. I think I removed the ph tags from top and bottom of the code. Now I just need to work out how to specify more than one keyword, can I just copy the last bit of code and change it to reflect other keywords and links?
              • 6511
              • 60 Posts
              danny_kay1710 Reply #56, 16 years ago
              I have modified it for a static array:
              <?php
              //$output = str_replace(' machinery safety', ' <b>machinery safety</b>', $modx->documentOutput);
                //$modx->documentOutput = $output;  
              
              $output = $modx->documentOutput;
                
              $rStart = strpos($output, '<!-- Keyword Replacement Start -->');
              $rStop =  strpos($output, '<!-- Keyword Replacement End -->');
              
              $rLength = $rStop - $rStart;  
                
              if (($rStop !== false) && ($rStart !== false))
              {   
                  if (!function_exists('str_replace_once')) {
                      function str_replace_once($needle , $replace , $haystack){
                          // Looks for the first occurence of $needle in $haystack
                          // and replaces it with $replace.
                          $pos = strpos($haystack, $needle);
                          if ($pos === false) {
                              // Nothing found
                              return $haystack;
                          }
                          return substr_replace($haystack, $replace, $pos, strlen($needle));
                      }
                  }
                  
                  $subStr = substr($output, $rStart, $rLength);
                  $rSubStr = $subStr ;
                    
                  $keywords = "machinery safety,European Directive,CE Marking"; \\ ensure no spaces in before and after comma. Seperate keywords by commas
                  $array = explode(",", $keywords);
                  
                  print_r($array);
                    
                  foreach ($array as &$value) {
                    
                    $replacementText = ' <a href="'.$value.'">'.$value.'</a>'; // might need to replace spaces with something for the href bit.
                    
                    if (strpos($rSubStr, $replacementText ) === false)
                       $rSubStr = str_replace_once(' '.$value, $replacementText, $rSubStr);          
                    
                  }
                    
                  $modx->documentOutput = str_replace($subStr, $rSubStr, $output);
                    //$modx->documentOutput = $tv;
              }
              ?>​
              


              Hope this meets your requirements, I am still looking into the Template Variable route but not having much luck at the moment.
                • 7072 ☆ A M B ☆
                • 43 Posts
                why not just use the Autolink plugin? smiley

                http://modxcms.com/extras/package/?package=30
                  • 6511
                  • 60 Posts
                  danny_kay1710 Reply #58, 16 years ago
                  That link doesn’t work - and I am also using Revo which has a limited number of extras at the moment
                    • 7072 ☆ A M B ☆
                    • 43 Posts
                    Quote from: danny_kay1710 at Sep 02, 2010, 02:54 AM

                    That link doesn’t work - and I am also using Revo which has a limited number of extras at the moment

                    maybe this link works: http://modxcms.com/extras/package/30

                    well I haven’t tested it in revo yet, but in Evo 1.0.4 it worked fine with just a few easy tweaks.
                      • 6511
                      • 60 Posts
                      danny_kay1710 Reply #60, 16 years ago
                      Nope that link still doesn’t work

                      I found it via a search which shows this URL but your link didn’t work http://modxcms.com/extras/package/?package=30

                      It is not for Revo but I will take a look at it.