We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3071
    • 99 Posts
    Done that, same, many href links. Are you not having this problem then?
      • 6511
      • 60 Posts
      danny_kay1710 Reply #32, 16 years ago
      I did, but then I cleared my site cache and then reloaded my page (with CTRL+F5 so it didn’t use browser cache either).

      The way I understand it is the onParseDocument event is called multiple times depending on your document - in your case it gets called 9 times in my case it was 13.

      Just had a thought. It looks like you have put the padded spaces inside of the hyperlink tag so you would get <a href="whatever"> whatever </a>.

      Make sure the spaces are added into the second parameter outside of the tags.

      $output = str_replace_once(' machinery safety', ' <a href="mahcinery-safety">machinery safety</a>', $modx->documentOutput); // NOTE THE SPACES ARE OUTSIDE THE TAG 
                                                       ^space is here              ^not here       ^or here
                                  ^space is here   ^not here
      
        • 3071
        • 99 Posts
        Well..my site cache button is clicked on on individual documents, could you tell me exactly how to do this if I am wrong? Speak to me like an idiot, I really don’t mind! Also what do you mean by the spaces, can you give me an exact example of the correct version again, just in case?! Thanks!!
          • 3071
          • 99 Posts
          If I don’t leave space in the href tag then it won’t leave them on my rendered page, so cannot do that. Tried it and then instead of it just doing the first it linked every instance of the keyword and omited the gap. This is truly driving me crazy!!

          Cleared the cache now, found the button, same thing, 9 href tags in my page code. embarrassed
          Tried outside of the tag. SOrry didn’t quite know what you meant. Now this hyperlinks every entry of that keyword on the page instead of just the first...

          Oh, and it hyperlinks only the first 9 keywords of the page!! Then leaves the rest alone..
            • 6511
            • 60 Posts
            danny_kay1710 Reply #35, 16 years ago
            No you put the space before the <a but instead of after the tag.
            you want to replace ' whatever' with ' <a href="whatever">'
            your doing          ' whatever ' with '<a href="whatever"> whatever </a>'
            

            like I said this runs multiple times so your output of > whatever < matches what you want to replace hence it adds another tag.

            Your not removing the spaces just moving where you put it in HTML does that make sense?
              • 3071
              • 99 Posts
              Yes, thanks, I have done all that now! Still can’t get it to work, added other stuff to my last comment that I’ve been trying!
                • 6511
                • 60 Posts
                danny_kay1710 Reply #37, 16 years ago
                I know the reason for it, but I cannot think of a way around it at the moment.... I’m thinking.

                Basically it is running this code 9 times and each time it is finding the next instance of it and changing it to a link.

                Try

                <?php
                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));
                    }
                }
                
                $output = $modx->documentOutput;
                
                if (strpos($output, ' <a href="mahcinery-safety">machinery safety</a>') === false)
                {
                  $modx->documentOutput = str_replace_once(' machinery safety', ' <a href="mahcinery-safety">machinery safety</a>', $output);
                } else {
                  return $modx->documentOutput;
                }
                ?>
                

                  • 3071
                  • 99 Posts
                  But why 9? Can’t understand the reason for this arbitary figure!
                    • 6511
                    • 60 Posts
                    danny_kay1710 Reply #39, 16 years ago
                    Because your page fires this event 9 times. It might change if you added another chuck or snippet. It’s basically the number of times it calls this function and I believe it will vary from site to site - maybe even page to page if your templates differ sufficiently.

                    It will not always be 9 it wasn’t when it happened to me for instance.

                    [edit]
                    Also see last reply for something to try out.
                    [/edit]

                    [SECOND EDIT]
                    slight improvement - maybe just a readibility one but none the less it may help you understand what I have done a little more
                    <?php
                    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));
                        }
                    }
                    
                    $output = $modx->documentOutput;
                    $replacementText = ' <a href="mahcinery-safety">machinery safety</a>';
                    
                    if (strpos($output, $replacementText) === false)
                    {
                      $modx->documentOutput = str_replace_once(' machinery safety', $replacementText, $output);
                    } else {
                      return $modx->documentOutput;
                    }
                    ?>
                    

                    [/SECOND EDIT]
                      • 3071
                      • 99 Posts
                      That removes any link, so not even one show. shocked