We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3071
    • 99 Posts
    Tried the plugin, just got database error.
      • 6511
      • 60 Posts
      danny_kay1710 Reply #62, 16 years ago
      I am relatively happy with the plugin code above for now - it does what I need.

      The only real improvement is for some way of managing it through the manager without editing raw code.

      I am at work at the moment so unable to do much with it - I still want to try and get it working with Template Variables but I dunno if they have been initialised in the onParseDocument event
        • 3071
        • 99 Posts
        Yes I agree although I haven’t worked out how to specify multiple keywords, I do see more code you kindly provided so maybe you covered it there with an array. Editing raw code is not ideal as well as you say, we need a Wordrpress Smartlinks solution!!!
          • 6511
          • 60 Posts
          danny_kay1710 Reply #64, 16 years ago
          Yeah there is an array in there for you to specify multiple keywords
            • 3071
            • 99 Posts
               
                $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>';


            The only thing here is, I can see the 3 keywords that we are looking to replace - "machinery safety,European Directive,CE Marking"
            But their replacements, I only see - $replacementText = ’ <a href="’.$value.’">’.$value.’</a>’;

            Is this just saying ’place a href link and use that keyword’

            Can’t we specify an exact href link, as certain keywords will be hyperlinked to a page that isn’t necesarily based around that keyword. For example, I might want to specify the keyword ’michael jackson’ and hyperlink it to the ’www.exanple.com/80s-soul-music

            Another issue I found is it is not searching for an exact match. It placed a hyperlin on the keyword ’bet’ within a sentence that used the plural, so looked like this get you free bets here!!

              • 6511
              • 60 Posts
              danny_kay1710 Reply #66, 16 years ago
              The only way to get around the problem with exact matches is to add a space both before and after but then it wouldn’t match bet with a comma after it either.

              I will look into being able to specify an exact URL
                • 3071
                • 99 Posts
                I see what you mean, if I was manually inserting links I probably wouldn’t use one that had a comma after it in any case. I need to take another look at that plugin as well that has been posted, it didn’t work last time though.
                  • 6511
                  • 60 Posts
                  danny_kay1710 Reply #68, 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));
                          }
                      }
                      
                      $subStr = substr($output, $rStart, $rLength);
                      $rSubStr = $subStr ;
                        
                      $keywords = "machinery safety|machinery-safety.html,European Directive|european-directives.html,CE Marking|ce-marking.html";
                      $array = explode(",", $keywords );
                      
                  
                        
                      foreach ($array as &$value) {
                        
                        
                        $myValues = explode("|", $value); 
                        
                        $replacementText = ' <a href="'.$myValues[1].'">'.$myValues[0].'</a> ';
                        
                        if (strpos($rSubStr, $replacementText ) === false)
                           $rSubStr = str_replace_once(' '.$myValues[0].' ', $replacementText, $rSubStr);          
                        
                      }
                        
                      $modx->documentOutput = str_replace($subStr, $rSubStr, $output);
                  }
                  ?>​
                  


                  It fixes the bugs with the exact match but adds the problem of linking with punctuation which you said you didn’t mind.

                  I have also altered the keywords variable and array so that is uses the format of $keywords = "word1|link1,word2,link2";

                  Hope this helps
                    • 3071
                    • 99 Posts
                    Thanks very much, you are very kind. I will try this out now. I’d probably prefer it ignoring uppercase so it doesn’t attach a link onto h1, h2 tags etc. I.e. an exact match, let me see how this does, thanks again.
                      • 6511
                      • 60 Posts
                      danny_kay1710 Reply #70, 16 years ago
                      It’s not a problem.

                      I need something very similar to this anyway so in a way all this really is doing is helping me test it grin