We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9995
    • 1,613 Posts
    I need a script which adds the full URL to all <img> links.
    Probably use str_replace? I have no php skills.

    So basicly when the template has:
    <img src="assets or <img src="/assets

    then make it:
    <img src="http://www.domain.com/assets

    	
    function insert($body)
      {
        return str_replace('<img src="' with '<img src="http://www.domain.com/');
      }
    


    This is the full PHP script where it needs to be added somewhere:
    http://fiddle.jshell.net/fourroses666/byL5bpsp/

    Maybe someone can help me? [ed. note: fourroses666 last edited this post 11 years, 11 months ago.]
      Evolution user, I like the back-end speed and simplicity smiley
      • 14214
      • 299 Posts
      This page has some helpful info http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.makeurl

      If you are using a template for layout you could just add
      [[~[[+id]]? &scheme=`full`]]
        • 9995
        • 1,613 Posts
        Can this be used for Evolution? I'm on Evo sad



          Evolution user, I like the back-end speed and simplicity smiley
          • 14214
          • 299 Posts
          [(site_url)][~[*id*]~]
            • 9995
            • 1,613 Posts
            Hmm, how do I explain.

            When sending newsletters I can't add the base href.
            So the generated source needs the site_url.
            When I add and image into the Content it needs to add the site_url.
            This isn't possible in Evo 1.0.15.

            So when I send a newsletter the generated source needs to have the site_url before the img src="

            I was thinking to add code into the script when there is <img src="/assets or <img src="assets/ then add the site_url. But not on <a href=" or when there already is <img src="http://www.site.com/assets/..

            Correct me if I'm wrong, but I don't see adding [(site_url)][~[*id*]~] will fix the problem. Have you seen the PHP code?

            Somehow add [(site_url)] before <img src="assets/ or every <img src="/assets will do but how do I do this? I'm not a coder so its a bit hard for me.
              Evolution user, I like the back-end speed and simplicity smiley
              • 14214
              • 299 Posts
              Where is this layout/design/content? Are they in MODx documents? Is there another snippet running?
                • 3749
                • 24,544 Posts
                EmailResource will do this for you. I think Notify does as well.

                Here's some code from EmailResource that might be useful:

                /**
                     * Convert URL in any form to a fully qualified URL
                     *
                     * @param $base string - sites base URL
                     * @return string - full URL
                     */
                    public function fullUrls($base) {
                        /* extract domain name from $base */
                        $splitBase = explode('//', $base);
                        $domain = $splitBase[1];
                        $domain = rtrim($domain,'/ ');
                
                        /* remove space around = sign */
                        //$html = preg_replace('@(href|src)\s*=\s*@', '\1=', $html);
                        $html = preg_replace('@(?<=href|src)\s*=\s*@', '=', $this->html);
                
                        /* fix google link weirdness */
                        $html = str_ireplace('google.com/undefined', 'google.com',$html);
                
                        /* add http to naked domain links so they'll be ignored later */
                        $html = str_ireplace('a href="' . $domain, 'a href="http://'. $domain, $html);
                
                        /* standardize orthography of domain name */
                        $html = str_ireplace($domain, $domain, $html);
                
                        /* Correct base URL, if necessary */
                        $server = preg_replace('@^([^\:]*)://([^/*]*)(/|$).*@', '\1://\2/', $base);
                
                        /* handle root-relative URLs */
                        $html = preg_replace('@\<([^>]*) (href|src)="/([^"]*)"@i', '<\1 \2="' . $server . '\3"', $html);
                
                        /* handle base-relative URLs */
                        $html = preg_replace('@\<([^>]*) (href|src)="(?!http|mailto|sip|tel|callto|sms|ftp|sftp|gtalk|skype)(([^\:"])*|([^"]*:[^/"].*))"@i', '<\1 \2="' . $base . '\3"', $html);
                
                        return $html;
                    }
                
                    /**
                     * Fix images attributes that confuse some mail rendering engines
                     */
                    public function imgAttributes() {
                        $html =& $this->html;
                        $replace = array (
                            '<img style="vertical-align: baseline;' =>'<img align="bottom" hspace="4" vspace="4" style="vertical-align: baseline;',
                            '<img style="vertical-align: middle;' => '<img align="middle" hspace="4" vspace="4" style="vertical-align: middle;',
                            '<img style="vertical-align: top;' => '<img align="top" hspace="4" vspace="4" style="vertical-align: top;',
                            '<img style="vertical-align: bottom;' => '<img align="bottom" hspace="4" vspace="4" style="vertical-align: bottom;',
                            '<img style="vertical-align: text-top;' =>'<img align="top" hspace="4" vspace="4" style="vertical-align: text-top;',
                            '<img style="vertical-align: text-bottom;' => '<img align="bottom" hspace="4" vspace="4" style="vertical-align: text-bottom;',
                            '<img style="float: left;' => '<img align="left" hspace="4" vspace="4" style="float: left;',
                            '<img style="float: right;' => '<img align="right" hspace="4" vspace="4" style="float: right;',
                        );
                        $html = $this->strReplaceAssoc($replace, $html);
                
                    }
                
                  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
                  • 14214
                  • 299 Posts
                  Bob will that code run in Evo?
                    • 3749
                    • 24,544 Posts
                    I don't see why it wouldn't.

                    BTW, you have to send the MODX base path as the argument to fullUrls.

                    You also have to have it inside a class and have a class variable, $this->html, which contains the content you want to modify.
                      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
                      • 14214
                      • 299 Posts
                      FourRoses - there ya go! As always, Bob to the rescue with great info