JoeW
JoeW

Reputation: 588

Parse text from database table as HTML in final rendered PHP page

One of the fields in our PHP page is a description - occasionally there are website links included. However, when pulling this data from the database table the links remain non-clickable. How can I parse text from database table as HTML in final rendered PHP page? Thanks!

Upvotes: 0

Views: 553

Answers (3)

Jochem
Jochem

Reputation: 2994

Do you mean something like this?

<?php
    echo "<a href='$linkLocation'>$linkName</a>";
?>

[Edit]

Server-side you could transform the orginal text ($det[9]) like this (based on this SO question):

<div id="text"><?php 
echo preg_replace_callback(
    '/http:\/\/([,\%\w.\-_\/\?\=\+\&\~\#\$]+)/',
    create_function(
        '$matches',
        'return \'<a href="http://\'.  $matches[1] .\'">\'.  $matches[1] .\'</a>\';'
    ),
    $det[9]
);
?></div>

[Edit: merged the original code from your comment] [Edit: fixed typo]

Upvotes: 1

vinay rajan
vinay rajan

Reputation: 361

Follow these steps

  1. Change the db feild to text,
  2. While inserting data convert the html entities and escape the quotes.
  3. while retrieving do the reverse process and you can retrieve data in html format this way

Upvotes: 0

Stefan
Stefan

Reputation: 5672

Are your text stored as HTML or is it just a text containing URLs?

Upvotes: 0

Related Questions