开发者

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

开发者 https://www.devze.com 2023-03-30 04:33 出处:网络
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 pa

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 P开发者_开发技巧HP page? Thanks!


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]


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消