We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37286
    • 160 Posts
    I am getting a little frustrated. I have developed a web program to help track progress notes. Most people are not having any problems, but a handful post notes and instead of an apostrophe we see things like "’" in the database. It appears to be something Google Chrome is doing, not sure if any others are. I am using a formit hook to pass values from the form to a snippet to save to the db. I thought xpdo was supposed to handle these types of things. Does anyone have a solution that wont interfere with what xpdo already does? I have attached a portion of my code for your review.

    //get values from post into array
    if($hook->getValue('rid')){
      $entry = $modx->getObject('Progress',$hook->getValue('rid'));
    }else{
      $entry = $modx->newObject('Progress');
    }
    $entry->fromArray($scriptProperties['fields']);
    
    //convert time to 24 hour format
    $entry->set('ProgStart',DATE("H:i", STRTOTIME($entry->get('ProgStart'))));
    $entry->set('ProgEnd',DATE("H:i", STRTOTIME($entry->get('ProgEnd'))));
    
    //get the difference between strating and ending
    $time_difference = $notefunc->get_time_difference( $entry->get('ProgStart') , $entry->get('ProgEnd') );
    $time_total = $time_difference['hours']*60 + $time_difference['minutes'];
    
    //make sure start time comes before end time within 8 hour period
    if( $time_total > 480 ){
    $hook->addError('error_message','Can not enter more than eight hours at a time.');
    return false;
    }
    
    /* save */
    if ($entry->save() == false) {
        $hook->addError('error_message','Unable to save entry to the database.');
    }
    
    • Looks like they're pasting from Word or some other rich-text type editor and the character being used by that editor is not being handled well by your database character set.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        I'm pretty sure that's an MS Word smart quote. In Word it renders as a left or right "curly" quote.

        If you plug this code in near the top, it should fix things:

        <?php 
        
        $entry = convert_smart_quotes($entry);
         
        function convert_smart_quotes($entry) 
        { 
            $search = array(chr(145), 
                            chr(146), 
                            chr(147), 
                            chr(148), 
                            chr(151)); 
         
            $replace = array("'", 
                             "'", 
                             '"', 
                             '"', 
                             '-'); 
         
            return str_replace($search, $replace, $entry); 
        } 
         
        ?>


        The last one converts an em dasy to a hyphen. You might rather convert it to "—" depending on how it will be used.
          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
          • 37286
          • 160 Posts
          After reading scottwells post earlier, I did some reading and found the same thing bobray posted. For some reason it didn't work, though I saw it on several websites. I did finally find what I thought was a fairly elegant solution, but if someone has something better I'm all ears.

          First I run through this, which works beautifully for removing my "smart apostrophes":
          $entry->set('ProgNotes', iconv('UTF-8', 'ASCII//TRANSLIT', $entry->get('ProgNotes') ) );
          


          Then just to make sure I get rid of all the obnoxious Microsoft formatting I put in a second line to get rid of everything else the first line may not change:
          $entry->set('ProgNotes', preg_replace('/[^(\x20-\x7F)]*/','', $entry->get('ProgNotes') ) );
          


          While I'm not 100% that this is the best approach, it is the only one that worked after several attempts to solve this annoying problem.

          Thanks for the feedback guys, it helped.
            • 36834
            • 25 Posts
            Try checking that your database character set, collation and connection are consistent. If your modx installation is using UTF8, check that your custom table is using the same charset and collation. If the problem is only appearing in the DB after the note is saved, then this could be the issue.

            Let me now if this helps ...
              • 3749
              • 24,544 Posts
              If you find that the charsets are not consistent, see this: http://bobsguides.com/convert-db-utf8.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