How to show actual html in a php page

Status
Not open for further replies.

Graham

New Member
Hi all,

I've got a quick question regarding php (newbie)

I'm using htmlspecialchar to convet html characters into their entities for storage in the DB, how ever I want the returned text to actually show the html.
I'm still at a learning stage so I've hand coded the following into the DB
<a href="www.somesite.ie">site</a>

however on using html_entity_decode I get back exactly this

<a href="somesite.ie">site</a>

Can anyone point out where I'm going wrong.

Cheers,


Graham
 

louie

New Member
Isn't that the right result?

I don't seem to follow you...
 

Graham

New Member
Sorry guys I'm not being clear,

I want the entities to be stored in the DB but when you read a post I actually want the HTML to become active.

For example I did up a quick form to insert the data incase I was incorrect in hand coding.

When inserting the data I call a function to clean the data as below

PHP:
function cleanData(variable)
{
 
 if(!get_magic_quotes_gpc())
 {
 
  variable=nl2br(variable);
 
  variable=addslashes(variable);
 
 variable= trim(variable);
 
 variable =htmlentities(variable);
 }
 
 return variable;
}

That will convert the html characters into their entites

When I want to show a post I have another function that is run to convert the data back to its original form (at least I think thats what its meant to do) but I'm getting for example

<br /> <br /> <br /> site<br /> <br /> <br /> <a href="http://www.irishwebmasterforum.com> site</a>

where I actually need the link to show and the <br/> tags to work.

Heres the function
PHP:
function showRowData(variabe)
{
    if(!get_magic_quotes_gpc())
    {foreach( variable as key  => value)
      {
         stripslashes(value);
 
        html_entity_decode(value);
      }
    } 
}

I can't seem to post the actual varible name so that is why I've used variable , key etc in the post

Hope this makes things clear, sorry if it doesn't have a cold and running on half a brain :)
 

louie

New Member
try this:
Code:
function un_html_data(value){
  value = html_entity_decode(value);
  return value;
}
//if that doesn't work try
function ewd_clean_html(str){
  find = array("#&gt;#","#&lt;#","#&quot;#");
  replace = array(">","<",""");
  str = preg_replace(find,replace,str);
  return str;
}
 

Graham

New Member
Thanks Louie,

I'll give it a try later on and see what happens and post back.


Thanks for the help.


Graham
 

Graham

New Member
Thanks Louie

Hi Louie,

Thanks for the help I got it working with the first one you gave me, I left out part of my code that said it was returning the data for the post from a query whose result set is using extract($row) to allow me access the variable by its name, I was just wondering if there is anyway to do this within a loop when extracting the data instead of having to manually place it in the echo statements. Again sorry for not posting all of the code required half asleep here.

PHP:
while(row = mysql_fetch_array())
  { extract(row);
    // showRowData();
     echo("<h1>".postTitle."</h1>");
   echo("<h2> Author: ".userName."</h2>");
   echo("<p class='postDate'> Post Date: ".Date."</p>");
   echo("<p>".un_html_data(postText)."<br/>"."</p>");
   echo("<p class='postTag'> Tags: ".postTags."</p>"); 
   }

Cheers, I owe you one.

P.S. The functionality is working but to put it hyperlinks I'm having to type <a href="http://www.irishwebmasterforum">Thanks Louie</a> do you have any links to sites which will teach me how to place them in automatically like the post editor on this site does?
 

louie

New Member
you can use preg_match to get the url and convert it to proper html link(s)
PHP:
preg_match("/^(http(s?)://|ftp://{1})((w+.)+)w{2,}(/?)$/i",value ,matches);
value= str_replace(matches[1],"<a href="".matches[1]."">".matches[1]."</a>",value);
you an ad this to the bottom of your un_html_data function
 

lukeat

New Member
that's interesting to see the html special chars code. not done it that way before, i uusually jus tuse mysql_escape and let it keep the html enttites.

grr, why can't i use the quickpost box??
 
Status
Not open for further replies.
Top