How to get html code from Wordpress post content? - php

I want to wrap decode all html tags from a post content in Wordpress.
like
<iframe src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2FRocketsAreCool%2Fvideos%2F1032994553496742%2F&show_text=0&width=560" width="560" height="315" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true">
When I try this code:
$query = new WP_Query(array('name'=>'example_topic'));
$post = $query->posts;
$text = htmlspecialchars_decode($post->content);
It works, but not closing the html tag because the tag was not originally closed in the encoded tags, so every thing comes after not showing in browser.

Related

php regex to change the value html attribute

I have a html string of iframe where width and it's value is included. I want to replace the width's value by regex in php. For example, I am getting a value dynamically as
<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>
I want to change the value of width by the regular expression. Can you help me someone.
Avoid using RegEx in XML/HTML documents, there are a performant libraries to do that, unless there a very very very good reason for that
Try with this code to achieve your job
<?php
$html = '<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$elements = $doc->getElementsByTagName('iframe');
foreach($elements as $el) {
$el->setAttribute('width', '1024');
}
print $doc->saveHTML();
OUTPUT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><iframe width="1024" height="315" src="" frameborder="0" allowfullscreen></iframe></body></html>
sounds like a really bad idea, but here goes... something like
<?php
header('content-type:text/plain;charset=utf8');
$str=base64_decode('PGlmcmFtZSB3aWR0aD0iNTYwIiBoZWlnaHQ9IjMxNSIgc3JjPSIiIGZyYW1lYm9yZGVyPSIwIiBhbGxvd2Z1bGxzY3JlZW4+PC9pZnJhbWU+');
$ret=preg_replace('/(\<iframe.*?width\=)\"(.*?)\"/','${1}"999"',$str);
var_dump($str,$ret);
will change width to 999... but you should really use a proper HTML parser instead, like DOMDocument.
$domd=#DOMDocument::loadHTML($str);
$domd->getElementsByTagName('iframe')->item(0)->setAttribute('width',999);
echo $domd->saveHTML($domd->getElementsByTagName('iframe')->item(0));
will also change width to 999, and is much more reliable (for example, the regex will break if there is spaces or newlines between the width and = , although it would still be legal html.. sigh)

Wordpress oEmbed not working when echoing Vimeo link

I'm creating a film portfolio and I have created the custom post type for portfolio items with a field for the Vimeo link. WordPress is not automatically embedding Vimeo links when I echo the url onto the page and is instead the url is displaying as plain text. I've tested by creating a post with the video link and that is auto-embedding just fine. Heres the code I'm using:
<?php echo get_post_meta(get_the_ID(), 'vimeo_link', TRUE); ?>
If I'm not mistaken, WordPress doesn't oEmbed inside of actual theme files. Instead, you'll need to do something like this
if (get_post_meta($wp_query->post->ID, 'vimeo_link', true) != '') {
<iframe src="<?php echo get_post_meta(get_the_ID(), 'vimeo_link', TRUE); ?>?title=0&byline=0&portrait=0" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
}
if statement reference

Insert tag html into MYSQL using PHP (Codeigniter)

I want to insert tag html (embed from youtube into Mysql) using Codeigniter like
<iframe width="560" height="315" src="https://www.youtube.com/embed/7jE-OcfMU2Y" frameborder="0" allowfullscreen></iframe>
But in the value in the table like
<iframe width="560" height="315" src="https://www.youtube.com/embed/7jE-OcfMU2Y" frameborder="0" allowfullscreen></iframe>
What sholud I do to solve it?
This is my controller, but it's still not work properly
$emb=str_replace('<', '<', $this->input->post('embed'));
data = array(
'content' => $this->input->post('judul'),
'embed' => $emb,
'category' => 'video',
);
$this->m_admin_tb_website->update_data('video', $data);
Did you try using htmlspecialchars_decode function?
Try this code:
$emb = htmlspecialchars_decode($this->input->post('embed'));
It works fine for me when I need to do the same you want.

How to convert youtube embed code to url?

There are a lot of examples of how to convert a youtube url to embed code, but I need a reverse code. I was never successful with all those expressions, so my question is How can I convert an embed code to a URL ? Thanks in advance.
In PHP, you can do that with DOMDocument to get the src attribute of the iframe, and preg_replace() to convert it to the video url:
$embed = '<html><head></head><body><p>Hello, there is the video <iframe width="560" height="315" src="http://www.youtube.com/embed/K75a2k_6QWs" frameborder="0" allowfullscreen></iframe> what do you think?</p></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($embed);
while($iframe = $doc->getElementsByTagName('p')->item(0)->getElementsByTagName('iframe')->item(0)) {
$url = preg_replace(
'~/embed/~',
'/watch?v=',
$iframe->getAttribute('src')
);
$iframe->parentNode->replaceChild(
$doc->createTextNode($url),
$iframe
);
}
echo $result = $doc->getElementsByTagName('p')->item(0)->nodeValue; //'Hello, there is the video http://www.youtube.com/watch?v=K75a2k_6QWs what do you think?'

php trim away undesired texts

I have the following data in mysql database which is an iframe from u-tube:-
`<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>`
Nevertheless, in the php codes, a present phase is present for the above iframe, in which I just need Om2fabTIKE4 as the variable to be filled in.
I would like to ask, is there any way that I can trim away
<iframe width="560" height="315" src="http://www.youtube.com/embed/
and
frameborder="0" allowfullscreen></iframe>
Try this:
$html = '<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('iframe');
foreach ($tags as $tag)
$link = explode('/',parse_url($tag->getAttribute('src'),PHP_URL_PATH));
var_dump($link[2]);
You can learn more about the DOMDocument class here.
Use str_replace to remove the text

Categories