HTML source code being printed as raw data from database - php

I save html in my database and when I recover this html in my view I see it like text in my browser and not html tags.
Example :
I save "<div><h1>Hello</h1></div>"
But when I echo it, the browser prints the tags as raw data, and not as HTML markup.

I guess you echo it like {{$string}}
This works same as e() function (PHP: htmlentities)
in order to echo html in Laravel Blade echo it like this {!! $string !!} to pass over HTML clean

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}

I've found what I want ! Juste use htmlspecialchars_decode() and it works !

Related

How to display ckeditor 5 data

I am trying to display data of CK editor 5 in frontend like this
enter image description here
The image you provided looks like unparsed markdown. In order to convert this into the correct HTML, you'll need to run it through a markdown parser first. In Laravel, you can actually use the built-in parser Laravel uses for emails. In your view, simply wrap the output with Illuminate\Mail\Markdown::parse(). For example, if you are currently using {{ $post->content }}, then you can replace it with:
{!! Illuminate\Mail\Markdown::parse($post->content) !!}
The {!! is to prevent blade from escaping the resulting HTML.

TinyMce not storing html, just raw text (laravel)

So i added TinyMCE with this method
<script src="https://cdn.tiny.cloud/1/myapihere/tinymce/5/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>
and added a textarea later on the text. But for some reason
This is what i want it to show to me, when the post is updated
This is what it shows me
If I understand correctly, your problem is that when you output the string from tinyMCE, you get the raw html without any formatting.
I think the problem is how you output the string. When outputting HTML in a blade template, don't use {{ $content }}, this will automatically encode html entities.
To output HTML, you have to use {!! $content !!}. This will output your string as is and won't parse html entities.

How to remove all html in post content and only trim text in php?

Currently i am doing a CMS in laravel currently i am trying to get all the html removed from posts.
{!! str_limit($post->content, 230) !!}
The problem i am getting is that it gets html as well.
Next i tried Strip_tags function, it didn't display the html.
{!! strip_tags(str_limit($post->content, 230)) !!}
But it didn't displayed anything either..it worked but it counted the html tags as well. I need to just ignore html tags.
Is there any way or function to ignore all the html tags inside the post content and just get the text out of post content?
Thanks
Any help will be appreciated!
Simple use strip_tags with substr
<?php echo substr(strip_tags($post->content,0,110)) . "..."; ?>

Rendering HTML from database table to view blade issue

I am having a problem by rendering some html stuff from a database table. I have a function that is calling and returning some html content from databse table, when i use {{ }} double curly braces it shows the content on page but as a plain text not rendered as html. After i try to use {!! !!} it does not show anything on page. i don't understand why and what's the solution in this case. My blade page contains the .blade extension as well.
Please advice.
$string = "<h1>Its H1 Tag</h1>";
{{ $string }}
Displaying Unescaped Data
By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
if above not work then use below but not a good practice-
in your case you can use htmlentities($string), html_entity_decode($string) on your data and then use {{ $string }} to render html.
For Laravel Version 5.6.* or higher use single Curley Braces
$string = "<h1>Its H1 Tag</h1>;
{!! $string !!}
It can be displayed by the following code too if above code doesn't work.
#php
echo $string;
#endphp

Escaping a string with Blade, rendering HTML

I have a $text variable that holds some text and HTML code. I would like to render its HTML, but still make sure the rest of the string is escaped.
$text = 'Example text with image <img src"image_1.jpg">. More text...'
// This will render the HTML but will NOT escape the string
{!! $text !!}
// This will escape and display the variable as raw string, with no HTML rendering
{{ $text }}
In Laravel 5, is there a way to escape a string with Blade while allowing HTML?
I am not aware of such functionality in blade. You can not just allow specific HTML tags, because they may have unwanted stuff (like onload) inside them. Processing of string may be tricky. Have you considered markup language?
In this case you need to do some manual processing of the string.
It might be possible that Purifier https://github.com/mewebstudio/Purifier will do what you need. It will make strings harmless and then it is safe to print the html in it.
Otherwise you need to write your own parser if nothing else like that is found.

Categories