We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    HI there,

    I have a lot of images which have spaces parenthesis in the file names - but I'm not allowed to change the file names, and there are too many to do manually anyway.

    Modx seems to have problems displaying the images on pages if there are spaces, so I created a snippet which replaces the space with %20 - which seems to work:

    <?php
    return str_replace(' ','%20',$input);
    


    But The only problem I have now is that quite a lot of the images also have parenthesis - e.g. this-file-name(v2).jpg
    I thought that this would be OK, because I can view the image directly in the browser, but when it passes through modx, it seems to break.

    Is there a way to enable parenethesis in the image urls, or do I need to convert them too?

    I thought I could add further lines to my snippet to encode the parenthesis like so:

    <?php
    return str_replace(' ','%20',$input);
    return str_replace('(','(',$input);
    return str_replace(')',')',$input);
    
    


    But this doesn't work - I'm no PHP programmer, so I assume the syntax is wrong?

    Can any one help?

    Thanks in advance

    Andy

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 51354
      • 11 Posts
      Hi Andy,

      I might know a solution. In your example you return in the first line. This way the other code doesn't get executed.

      Try something like this:

      $input = str_replace(' ','%20',$input);
      $input = str_replace('(','(',$input);
      return str_replace(')',')',$input);
      
        • 51020
        • 670 Posts
        Quote from: andreasf at Dec 12, 2017, 02:10 PM
        Hi Andy,

        I might know a solution. In your example you return in the first line. This way the other code doesn't get executed.

        Try something like this:

        $input = str_replace(' ','%20',$input);
        $input = str_replace('(','(',$input);
        return str_replace(')',')',$input);
        

        Ah Ok - as you can see, I know 0.1% of php!! Thank - I'll give that a go.
          • 3749
          • 24,544 Posts
          Your lines 1 and 2 don't actually do anything, since they replace the character with itself. (though maybe the Forum mangled your original code).

          Parentheses are allowed in URLS. What do you mean by 'when it passes through MODX, it seems to break'?
            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
            • 51020
            • 670 Posts
            Quote from: BobRay at Dec 12, 2017, 03:06 PM
            Your lines 1 and 2 don't actually do anything, since they replace the character with itself. (though maybe the Forum mangled your original code).

            Parentheses are allowed in URLS. What do you mean by 'when it passes through MODX, it seems to break'?

            Thanks bob.
            I was trying to encode the parentheses with an html entity because for some reason all the images with file names such as file-name(123).jpg aren’t loading from a tv. Could this be because I’m running it through pthumb? Do I need an output modifier?


              • 51020
              • 670 Posts

              Quote from: BobRay at Dec 12, 2017, 03:06 PM
              Your lines 1 and 2 don't actually do anything, since they replace the character with itself. (though maybe the Forum mangled your original code).

              Parentheses are allowed in URLS. What do you mean by 'when it passes through MODX, it seems to break'?

              Actually it’s. Or using pthumb at all. It’s a straight line tv in a placeholder for getResources.
              Basically it’s pulling in the product thumbnails.
              • discuss.answer
                • 3749
                • 24,544 Posts
                It sounds like pThumb is messing with the parentheses, maybe by calling PHP's urlEncode().

                If that's the case, this code from andreasf should work, as long as the ## strings are replaced by whatever you see in the URL where the parentheses should be.

                $input = str_replace(' ','%20',$input);
                $input = str_replace('##','(',$input);
                return str_replace('##',')',$input);


                  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
                  • 51020
                  • 670 Posts
                  Quote from: BobRay at Dec 13, 2017, 11:34 AM
                  It sounds like pThumb is messing with the parentheses, maybe by calling PHP's urlEncode().

                  If that's the case, this code from andreasf should work, as long as the ## strings are replaced by whatever you see in the URL where the parentheses should be.

                  $input = str_replace(' ','%20',$input);
                  $input = str_replace('##','(',$input);
                  return str_replace('##',')',$input);



                  Yep that worked - thank you Bob!
                    • 3749
                    • 24,544 Posts
                    I'm glad you got it sorted, but andreasf deserves the credit. smiley
                      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
                      • 51020
                      • 670 Posts
                      Quote from: andreasf at Dec 12, 2017, 02:10 PM
                      Hi Andy,

                      I might know a solution. In your example you return in the first line. This way the other code doesn't get executed.

                      Try something like this:

                      $input = str_replace(' ','%20',$input);
                      $input = str_replace('(','(',$input);
                      return str_replace(')',')',$input);
                      

                      Thanks Andreas!!