3 Replies Latest reply on Mar 8, 2012 2:00 PM by Oliver_Reid

    PHP -  rendering quotes from text question

    Oliver_Reid

      As a dillettante PHP programmer, I am having trouble "gettting" htmlentities.

       

      In a record in an ESS MySQL table I want tehuser to able to insert quotes (incudeing smart quotes) usng FMP and smart quotes and hav eth appear on PHP driven site,

       

      But I want to be able also to insert links (which require quotes inside the tags) and other html markup have those work.

       

      I have seen this work so there is away to do it

       

      If I use

       

      $content = str_replace(" ", '<br>', htmlentities($row['b_text'], ENT_NOQUOTES, 'cp1251'));

       

      The quotes come through but the links and markup are rendered literally.

       

       

      Help? Bev are you there?

        • 1. Re: PHP -  rendering quotes from text question
          beverly

          I wonder if you need htmlspecialchars, instead of htmlentities

               http://www.php.net/manual/en/function.htmlspecialchars.php

           

          And your "\r" should probably be "\n", but I don't know for sure with the encoding you've got (cp1251).

           

          (not tested),

          Bev

          • 2. Re: PHP -  rendering quotes from text question
            Oliver_Reid

            I tried that but could not get it to work

             

            Here is an example where it works:

             

            http://www.beachsidesoccer.org/popups/news-popup.php?rid=87&

             

            One where it does not

             

            http://edpsoccer.org/popups/news-popup.php?rid=86&

             

            I am trynt simply setal code form one site and use it in another.

             

            Code for FIRST  (at present I am not using html entities in either, but can't see why one works and one does not.)

             

            <?php

            $errors = '';

            require_once($_SERVER['DOCUMENT_ROOT'] . '/connections/dbfunclib.php');

             

             

            if (!isset($_GET['rid']) || !is_numeric($_GET['rid'])){

                      $errors = 'Could not find article';

                      $title = 'ERROR';

            } else {

                      $rid = $_GET['rid'];

             

                      $db = new VCIDB();

                      $sql = 'SELECT b_date, b_headline, b_text, b_photo_file_name, b_photo_url

                                          FROM news WHERE a_rec_id = ' . $db->SqlEscape($rid, 'int') . ' LIMIT 1';

                      $row = $db->FetchRow($sql);

                      if (!$row) {

                                $errors = 'Could not find article';

                                $title = 'ERROR';

                      } else {

                                $date = date('M jS Y', strtotime($row['b_date']));

                                $title = htmlentities($row['b_headline']);

                                $content = str_replace("\r", '<br>', $row['b_text']);

                                //$content = str_replace("\r", '<br>', htmlentities($row['b_text'], ENT_NOQUOTES, 'cp1251'));

                                $img = $row['b_photo_url'];

                      }

            }

            ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

            <html>

            <head>

            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

            <title><?=$title;?></title>

            <link href="/css/styles.css" rel="stylesheet" type="text/css"/>

             

             

            </head>

             

             

            <body style="background-color:#FFFFFF;">

                      <div style="padding:5px 10px 5px 10px;">

                                <?php

                                          if (!empty($errors)){

                                                    echo '<h1>' . $errors . '</h1>';

                                          } else {

                                ?>

                                <h1><?= $title; ?></h1><br/>

                                <?php if ($img) echo '<img src="'.$img.'" align="right" style="padding:0px 0px 10px 10px;">'; ?>

                                <p><strong><?= $date; ?></strong><br/><br/><?= $content ?></p>

                                <?php } ?>

                      </div>

            </body>

            </html>

             

             

            Code for SECOND

             

            <?php

            $errors = '';

            require_once($_SERVER['DOCUMENT_ROOT'] . '/connections/dbfunclib.php');

             

             

            if (!isset($_GET['rid']) || !is_numeric($_GET['rid'])){

                      $errors = 'Could not find article';

                      $title = 'ERROR';

            } else {

                      $rid = $_GET['rid'];

             

                      $db = new VCIDB();

                      $sql = 'SELECT b_date, b_headline, b_text, b_photo_file_name, b_photo_url

                                          FROM news WHERE a_rec_id = ' . $db->SqlEscape($rid, 'int') . ' LIMIT 1';

                      $row = $db->FetchRow($sql);

                      if (!$row) {

                                $errors = 'Could not find article';

                                $title = 'ERROR';

                      } else {

                                $date = date('M jS Y', strtotime($row['b_date']));

                                $title = htmlentities($row['b_headline']);

                                $content = str_replace("\r", '<br>',$row['b_text']);

                                //$content = str_replace("\r", '<br>', htmlentities($row['b_text'], ENT_NOQUOTES, 'cp1251'));

                                $img = $row['b_photo_url'];

                      }

            }

            ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

            <html>

            <head>

            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

            <title><?=$title;?></title>

            <link href="/bsc_css/styles.css" rel="stylesheet" type="text/css"/>

             

             

            </head>

             

             

            <body style="background-color:#FFFFFF;">

                      <div style="padding:5px 10px 5px 10px;">

                                <?php

                                          if (!empty($errors)){

                                                    echo '<h1>' . $errors . '</h1>';

                                          } else {

                                ?>

                                <h1><?= $title; ?></h1><br/>

                                <?php if ($img) echo '<img src="'.$img.'" align="right" style="padding:0px 0px 10px 10px;">'; ?>

                                <p><strong><?= $date; ?></strong><br/><br/><?= $content ?></p>

                                <?php } ?>

                      </div>

            </body>

            </html>

            • 3. Re: PHP -  rendering quotes from text question
              Oliver_Reid

              Fwiw I figured this out

               

              First a function:

               

              function fix_fancy_quotes($text) {

                        $ret = $text;

                        $ret = str_replace('&','&amp; ',$ret);

                        $ret = str_replace('‘','&lsquo;',$ret);

                        $ret = str_replace('’','&rsquo;',$ret);

                        $ret = str_replace('“','&ldquo;',$ret);

                        $ret = str_replace('”','&rdquo;',$ret);

                        return $ret;

               

              and then

               

              $content = str_replace("\r", '<br>',fix_fancy_quotes(nl2br($row['b_text'])));

               

              This allow embedded html in the text to function, and also deals with smart quotes.

              1 of 1 people found this helpful