-1

Sorry for my dumb question but I'm a rubyist and this is the first time I use php. I must clean using preg_replace some HTML code.

This is a sample piece of code:

        <span style="font-size: 13pt; color: rgb(85, 85, 85); font-family: helvetica; font-weight: bold; line-height: 17pt;">Come funziona il segnale televisivo?</span><br>
            Per capire cosa sono i 100 Hz e i 200 Hz dobbiamo fare un passo indietro e capire come funziona il sistema televisivo. "Interlacciato" indica il modo in cui queste linee vengono usate per comporre un fotogramma, ovvero una singola frazione d'immagine: <strong>anziché mostrare un fotogramma intero da 576 linee, quando il segnale è interlacciato vengono mostrati rapidamente e in successione le 288 linee pari e poi le 288 linee dispari.</strong> Il totale è sempre 576 ma, al posto di avere un solo fotogramma, avremo due mezzi-fotogrammi, chiamati tecnicamente semiquadri, che contengono la metà della risoluzione totale. Susseguendosi rapidamente, questi semiquadri ingannano l'occhio umano che li percepirà come un unico frame, ma si tratta sempre di 288 linee pari seguite da 288 linee dispari. Il disegno sotto aiuta a capire il concetto.<br>
        </span>
    </p>
    <p style="text-align: center;"><img width="560" height="375" alt="" src="http://www.dday.it/userFiles/FCK/foto1.jpg"></p>
    <p>
        <span style="font-size: 10pt; color: rgb(85, 85, 85); font-family: helvetica; font-weight: normal; line-height: 14pt;"> Il contrario di interlacciato è il “progressivo”: <strong>un’immagine progressiva è anch'essa composta da semiquadri, ma ciascuno di questi semiquadri è completo</strong>, quindi con 576 linee.</span>
    </p>
    <p style="text-align: center;"><img align="absMiddle" alt="" src="http://www.dday.it/userFiles/FCK/foto2(1).jpg"></p>
    <p>
        <span style="font-size: 10pt; color: rgb(85, 85, 85); font-family: helvetica; font-weight: normal; line-height: 14pt;"><br></span>
        <span style="font-size: 10pt; color: rgb(85, 85, 85); font-family: helvetica; font-weight: normal; line-height: 14pt;"> Veniamo ora alla frequenza di quadro: <strong>u</strong><strong>n secondo di video è composto da 25 fotogrammi che, nel caso dell’interlacciato, sono composti a loro volta da due semiquadri</strong>: 25 moltiplicato per due fa proprio 50 Hz.</span>
    </p>
    <p style="text-align: center;"><img width="500" height="200" alt="" src="http://www.dday.it/userFiles/FCK/foto-3.jpg"></p>
    <p>
        <span style="font-size: 10pt; color: rgb(85, 85, 85); font-family: helvetica; font-weight: normal; line-height: 14pt;"><br><br></span>
    </p>
</div>

When I try to update my db with a query I receive an error for the single quotes and the double quotes in my HTML. I know that I must replace single and double quotes, but If I use a regular expression like this:

$string = preg_replace("/\'/", '&#39;', $string);
$string = preg_replace('/\"/', '&quot;', $string);

The regex change also the quote inside my HTML tags and this broke my HTML. How can I avoid this? Is there any PHO istruction to convert only text inside tags?

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
Roberto Pezzali
  • 2,484
  • 2
  • 27
  • 56
  • 1
    Sounds like you're using string concatenation / interpolation to set your query values. **Don't do that**. Instead, use prepared statements and parameter binding. Transforming incoming data is a sure-fire way to lose valuable information – Phil Feb 03 '14 at 23:52
  • I think stripslashes is what you want : http://www.php.net/stripslashes - more or less magic. – Marc Feb 03 '14 at 23:56
  • @datamafia You're kidding, right? – Phil Feb 03 '14 at 23:57
  • Add/strip slash will balance. Not a solution to protect query/db. Also - for mixed single/double quotes see nowdoc syntax http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc - also html entities in PHP http://www.php.net/htmlentities – Marc Feb 04 '14 at 00:07
  • @datamafia All `stripslashes` does is remove escaping added by something like `magic_quotes_gpc`, both of which have absolutely no place in modern PHP. It also seems OP already has an HTML string in `$string` – Phil Feb 04 '14 at 00:10
  • Oh, @phil you are correct, I misread the last part. PS will solve the problem if there is no hardened framework to do the lifting. Sorry/thx. – Marc Feb 04 '14 at 00:16

1 Answers1

1

As mentioned, persist the original HTML string, as-is, safely and securely using a prepared statement. Here's a PDO example (will edit if it turns out you're using MySQLi but my assumption is that you're using the MySQL extension which you shouldn't be doing anyway)

$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'user', 'pass', [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);

$stmt = $pdo->prepare('UPDATE `someTable` SET `html` = ? WHERE `id` = ?');
$stmt->execute([$string, $someTupleIdentifier]);
Community
  • 1
  • 1
Phil
  • 157,677
  • 23
  • 242
  • 245