We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14281
    • 120 Posts
    It’s times like this when it’s difficult not to name a thread "What the Heck!?!" laugh

    I have a snippet running on my website called MakeURLSafe, which is designed to pass URLs and other address-embedded strings into, so that it can replace illegal or otherwise trouble-causing characters and symbols with URL entities, e.g. replacing spaces with "%20", qoutations with "%22", and so on. The snippet looks like this:

    <?php
    //takes &string as parameter
    	$search = array('"',':','/','?','#quest;','#equal;','=','&','#',' ');
    	$replace = array('%22','%3A','%2F','%3F','%3F','%3D','%3D','%26','%23','%20');
    	$string = str_replace($search,$replace,$string);
    	return $string;
    ?>
    


    It worked when I first created it, but sometime between that time (about a week or so ago) and last night, it stopped working. I ran this code by itself in a test php document, echoing the result instead of returning it, and it worked fine. The test string I used was

    Desktops for "Chipmunk Loses"


    The result was:

    Desktops%20for%20%22Chipmunk%20Loses%22


    That probably seems like an awfully arbitrary test choice. If you go to my site, it makes more sense why I chose it; it’s the title of my latest post, which might get embedded in a share link sent to Delicious or something.

    Anyway, the snippet works fine when ran solo in a test php document, and returns the expected result. But when I run it in as an actual snippet under MODx, it returns the string as it received it, completely unformatted. I ran some further tests and found that the string was, in fact, being successfully, completely, and accurately passed to the snippet, and I traced its path and found that it was also being passed all the way through the snippet successfully. But the return value doesn’t appear to be effected by the str_replace function.

    Any clues why this might be failing? All help much appreciated.
      • 18397
      • 3,250 Posts
      1. Are you calling the snippet cached or uncached. Try alternating between the two and see what happens.
      2. Try replacing the last 2 lines with:
      return str_replace($search,$replace,$string);
      

      3. To make something URL safe remember there are PHP functions (and a MODx one wink ) that handle that. I see you have additional items to search and replace for but you may want investigate letting one of the said functions handle the other items.
        • 27376
        • 576 Posts
        php.net/urlencode
        php.net/htmlentities
        php.net/html_entity_decode

        [tt]html_entity_decode[/tt] would be used to make the string plain text, then use [tt]urlencode[/tt] to translate the string to a url safe string.
          • 30223
          • 1,010 Posts
          I think the point here is that the input value doesn’t seem to be altered, regardless of what the snippet code itself does. I tried the snippet and called it like this [!MakeURLSafe? &string=`My "electric" blanket`!] and sure enough it outputs My%20%22electric%22%20blanket.

          This makes me wonder if you are feeding the snippet some placeholder or MODx tag? [!MakeURLSafe? &string=`[~122~]`!] for instance wouldn’t work, something easily overlooked if you’re not aware of this. This is because the placeholder/tag isn’t yet evaluated by the time your snippet is run. If this is the case try adding the [ & ] characters to your replacement array and see what happens smiley

            • 14281
            • 120 Posts
            Thanks a lot for the help. Calling the snippet uncached fixed it. Thanks Mark.

            And thanks for the urlencode tip, sirlancelot. I’ve been developing in php for several years, and I never thought to see if there was an existing function for this. However, it doesn’t do the trick for this snippet. I’m not sure why, but when I replaced my custom code with

            return urlencode($string);


            The string that was returned caused problems for some sites that the final URL was sent do, such as Digg. I’m guessing that these sites don’t unencode certain URL entities.

            Quote from: TobyL at Apr 06, 2007, 12:14 PM

            I think the point here is that the input value doesn’t seem to be altered, regardless of what the snippet code itself does. I tried the snippet and called it like this [!MakeURLSafe? &string=`My "electric" blanket`!] and sure enough it outputs My%20%22electric%22%20blanket.

            This makes me wonder if you are feeding the snippet some placeholder or MODx tag? [!MakeURLSafe? &string=`[~122~]`!] for instance wouldn’t work, something easily overlooked if you’re not aware of this. This is because the placeholder/tag isn’t yet evaluated by the time your snippet is run. If this is the case try adding the [ & ] characters to your replacement array and see what happens smiley



            Well, I’m not submitting placeholders to the snippet, like "[+placeholder+]", but I am submitting document tags, like "[*longtitle*]", and I’m even submitting custom TVs, like "[*CustomKeywords*]", (if I’m using the right terminology, but you get my point). That actually works just fine. All the tags I submit get evaluated before they get submitted to the snippet. It was calling the snippet cached that was causing the big problem.

            Again, thanks a lot to all of you for lending a hand. The hive wins again. smiley