How can I show php variable as html? - php

I am trying this code:
<?php $img = get_post_meta($post->ID, "imagenes", $single = true);
echo "$img";
?>
Where $img= <img src="https://www.newtvguide.com/wp-content/uploads/2020/05/MiamiDMT-drk-purp-cover.jpg">
But on output, it's showing the element as text, all html come in texts like this: <img src="https://www.newtvguide.com/wp-content/uploads/2020/05/MiamiDMT-drk-purp-cover.jpg">It's coming whole html code as text, I want it html output ....... (You can see it here: https://www.ebmovies.icu/dragon-soldiers/
It's not coming in html, I want to show it as image and there will be a link on image.
So is there any way to output the code as html not just texts?

Related

php - preg_replace is duplicating the tags and not replacing them

Trying to look inside my imap messages and if any img tags are found replace them with a new link.
have at look here:
http://codepad.viper-7.com/WGxtw7
So far I have
$message = imap_fetchbody($imap,$email_number,"2"); //2
$message = quoted_printable_decode($message);
$img_replace = $message;
$img_replace = preg_replace('~src="cid.*?"~',"src=\"new-link\"", $img_replace);
echo $img_replace;
With this code I get the img tags with the new links however, instead of being replaced they are duplicate so my final output is showing both links and imgs.
the generated message will show something like the html below, the src, width and height are always different and just the src needs to be replaced(no duplicated) with a new link.
<h2>Message: <div dir="ltr">this is a test with 2 inline images<div><br></div>
<div><img src="cid:ii_iaa9ibac0_14da215402096e51" width="544" height="340"><br><br></div>
<div><br></div><div><img src="cid:ii_iaa9inn31_14da2157f7bb06e9" width="405" height="405"><br><br></div></div>
</h2>
It should look like:
<h2>Message: <div dir="ltr">this is a test with 2 inline images<div><br></div>
<div><img src="new-link" width="544" height="340"><br><br></div>
<div><br></div><div><img src="new-link" width="405" height="405"><br><br></div></div>
</h2>
So what I was thinking is to get a function that will:
Look inside the final html
if img src attributes start with 'cid:'
replace the src link with 'new-link'
Any Idea?

Remove HTML code from content printed by wordpress

I am printing out the content of a specific post (103 in this case) with the following code:
<?php $post_id = 103; $queried_post = get_post($post_id); echo apply_filters('the_content',$queried_post->post_content); ?>
How can I make it exclude all the html tags like <p> and <br>s, and just show the text?
Thanks!
Use strip_tag(string) function to remove all html tags from string
echo strip_tags(apply_filters('the_content',$queried_post->post_content));

Why is a specific variable causing html to print out

I am trying to display some images in my wordpress site. I am trying to make the background div and image via style=""
So when I do this:
<?php
if(has_post_thumbnail($property->ID)){
$image_url = get_the_post_thumbnail($property->ID,array(300,220),array('class' => "post_thumbnail"));
}else{
$image = $image_url = '';
}
?>
<div class="property_photo" style="background-image:url('<?php echo $image_url;?>') no-repeat;">
</div>
The photo is not displaying anymore and it's printing ') no-repeat;"> where the image should display. When I use a variable without an "_" in the name, it doesn't print anything. So to avoid that I tried $image = $image_url = ''; to get around that.
Is this is a simple syntax problem or is there something in the php that is causes this? It doesn't seem like syntax because when I use other variable it does not do this.
Your get_the_post_thumbnail() function is returning HTML, not a URL. You need to either change the function to only return a URL (or use a different function which does that), or change your HTML to put the image inside the div instead of setting it as a background image.

Select Content of div using php

I have a div named "main" in my page. I put the code to convert a html into pdf using php at the end of page. I want to select the content (div named main contains paragraphs, charts, tables etc.).
How ?
Below code will show you how to get DIV tag's content using PHP code.
PHP Code:
<?php
$content="test.html";
$source=new DOMdocument();
$source->loadHTMLFile($content);
$path=new DOMXpath($source);
$dom=$path->query("*/div[#id='test']");
if (!$dom==0) {
foreach ($dom as $dom) {
print "
The Type of the element is: ". $dom->nodeName. "
<b><pre><code>";
$getContent = $dom->childNodes;
foreach ($getContent as $attr) {
print $attr->nodeValue. "</code></pre></b>";
}
}
}
?>
We are getting DIV tag with ID "test", You can replace it with your desired one.
test.html
<div id="test">This is my content</div>
Output:
The Type of the element is: div
This is my content
You should put the php code into a separate file from the html and use something like DOMDocument to get the content from the div.
$dom = new DOMDocument();
$dom->loadHTMLFile('yourfile.html');
...
You cannot directly interact with the HTML DOM via PHP.
What you could do, is using a with an input containing your content. When submitting the form you can access the data via PHP.
But maybe you want to use Javascript for that task?
Nevertheless, a quick'n'dirty PHP example:
<form action="" method="post">
<textarea name="content">hello world</textarea>
</form>
<?php
if (isset($_POST['content'])) {
echo $_POST['content'];
}
?>

HTML manipulation: Match the first X number of HTML tags and move them

Let's say I've have the code like this:
<img src="001">
<img src="002">
<p>Some content here.</p>
<img src="003">
What I want to do now is to match the first two images (001 and 002) and store that part of the code in variable. I don't want to do anything with third image.
Id used something like preg_match_all('/<img .*>/', $result); but it obviously matched all the images. Not just those which appear on the top of the code. How to modify that regular expression to select just images that are on top of the code.
What I want to do is to now. I've have <h2> tag with title in one variable and the code above in the second. I want to move the first X images before the <h2> tag OR insert that <h2> tag after first X images. All that in back-end PHP. Would be fun to make it with CSS, but flexbox is not yet here.
You need to divide the problem to solve it. You have got two main parts here:
Division of the HTML into Top and Bottom parts.
Doing the DOMDocument manipulation on (both?) HTML strings.
Let's just do that:
The first part is actually quite simple. Let's say all line separators are "\n" and the empty line is actually an empty line "\n\n". Then this is a simple string operation:
list($top, $bottom) = explode("\n\n", $html, 2);
This solves the first part already. Top html is in $top and the rest we actually do not need to care much about is stored into $bottom.
Let's go on with the second part.
With simple DOMDocument operations you can now for example get a list of all images:
$topDoc = new DOMDocument();
$topDoc->loadHTML($top);
$topImages = $topDoc->getElementsByTagname('img');
The only thing you need to do now is to remove each image from it's parent:
$image->parentNode->removeChild($image);
And then insert it before the <h2> element:
$anchor = $topDoc->getElementsByTagName('h2')->item(0);
$anchor->parentNode->insertBefore($image, $anchor);
And you're fine. Full code example:
$html = <<<HTML
<h2>Title here</h2>
<img src="001">
<p>Some content here. (for testing purposes)</p>
<img src="002">
<h2>Second Title here (for testing purposes)</h2>
<p>Some content here.</p>
<img src="003">
HTML;
list($top, $bottom) = explode("\n\n", $html, 2);
$topDoc = new DOMDocument();
$topDoc->loadHTML($top);
$topImages = $topDoc->getElementsByTagname('img');
$anchor = $topDoc->getElementsByTagName('h2')->item(0);
foreach($topImages as $image) {
$image->parentNode->removeChild($image);
$anchor->parentNode->insertBefore($image, $anchor);
}
foreach($topDoc->getElementsByTagName('body')->item(0)->childNodes as $child)
echo $topDoc->saveHTML($child);
echo $bottom;
Output:
<img src="001"><img src="002"><h2>Title here</h2>
<p>Some content here. (for testing purposes)</p>
<h2>Second Title here (for testing purposes)</h2>
<p>Some content here.</p>
<img src="003">

Categories