John Davidson

php - How to convert HTML content to string in cakephp?

0 comments
Message:


I am building an application using cakephp v4.


Should save JSON object including HTML content type while creating an event in MS graph api.



$description = '<span class="is-size-6" style="font-style: normal; font-weight: 400;">Microsoft Teams Meeting</span><span style="font-style: normal; font-weight: 400;"></span><div class="is-flex is-flex-direction-column mt-3"><span style="font-weight: 700;">The meeting url will be added here.</span><a href="https://aka.ms/JoinTeamsMeeting" target="_blank">Learn More</a></div>';

$event = '{
"subject": "Hello World",
"body": {
"contentType": "HTML",
"content": "' . h($description) . '",
},
...
}';

If I fetch the saved event, then the body->content looks like:


<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\n&lt;span class=&quot;is-size-6&quot; style=&quot;font-style: normal; font-weight: 400;&quot;&gt;Microsoft Teams Meeting&lt;/span&gt;&lt;span style=&quot;font-style: normal; font-weight: 400;&quot;&gt;&lt;/span&gt;&lt;div class=&quot;is-flex is-flex-direction-column mt-3&quot;&gt;&lt;span style=&quot;font-weight: 700;&quot;&gt;The meeting url\r\n will be added here.&lt;/span&gt;&lt;a href=&quot;https://aka.ms/JoinTeamsMeeting&quot; target=&quot;_blank&quot;&gt;Learn More&lt;/a&gt;&lt;/div&gt;\r\n</body>\r\n</html>\r\n"

I want to show the HTML content in textarea element.


What I tried is to use php htmlentities function.


But it still has HTML tags like the following image.



How to show only strings like the first image?



My Answer:
To convert HTML content to a string in CakePHP, you can use the `strip_tags` function in PHP. Here's how you can modify your code to achieve this:

php
$description = 'Microsoft Teams Meeting
The meeting url will be added here.Learn More
';

$event = '{
"subject": "Hello World",
"body": {
"contentType": "HTML",
"content": "' . strip_tags($description) . '",
},
...
}';


By using `strip_tags` function, you can remove all HTML tags from the `$description` variable and only keep the text content. This will allow you to display the content as a string without any HTML tags in a textarea element.

Rate this post

4 of 5 based on 5082 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us