Adding an ::after pseudo-element to HTML Generated in PHP - php

I currently have HTML generated by PHP which looks like this:
echo '<div class="right">';
echo '<p class = "triangle-isosceles left">'; the_sub_field("right_quote", false); echo '::after'; echo '</p>';
echo '</div>';
Which currently displays in html as:
<p>"This is the output of the_sub_field function ::after"</p>
When I want it to output as
<p>"This is the output of the_sub_field function" ::after</p>

a ::after or ::before are a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML).
While the end result is not actually in the DOM, it appears on the page as if it is, and would essentially be like this:
div::after {
content: "hi";
}
So to add a pseudo you must use css, try this :
echo '<div class="right">';
echo '<p class = "triangle-isosceles left">'; the_sub_field("right_quote",
false); echo '</p>';.
echo '<style>p.triangle-isosceles:after{content:"something"}</style>';
echo '</div>';
then the result would be like :
p.triangle-isosceles:after{
content:" -> after content";
}
<p class="triangle-isosceles">"This is the output of the_sub_field function"</p>

You cannot add an :after element inside your DOM (your current loaded page) you have to add it to your stylesheet file on your <p> element using his classes.
like this
.triangle-isosceles:after{
content:'';
}
See this : https://www.w3schools.com/cssref/sel_after.asp
Of course you can use the php function fopen and fwrite and fclose to add your new css rules.
<php?
// This is your after element you put in a variable (like a storage place)
$addMyAfterElement = ".triangle-isosceles:after{ content:'';";
// This is your stylesheet file you open and you put in a variable
$myStyleSheet = fopen("styles\style.css", 'a+');
// This is writting your after element inside the stylesheet file
fwrite($myStyleSheet, $addMyAfterElement);
// And here you close your stylesheet file
fclose($myStyleSheet);
?>

Related

Change src atribute from img, using Simple HTML Dom php library

I'm totally new to php, and I'm having a hard time changing the src attribute of img tags.
I have a website that pulls a part of a page using Simple Html Dom php, here is the code:
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.tabuademares.com/br/bahia/morro-de-sao-paulo');
foreach($html ->find('img') as $item) {
$item->outertext = '';
}
$html->save();
$elem = $html->find('table[id=tabla_mareas]', 0);
echo $elem;
?>
This code correctly returns the part of the page I want. But when I do this the img tags comes with the src of the original page: /assets/svg/icon_name.svg
What I want to do is change the original src so that it looks like this: http://www.mywebsite.com/wp-content/themes/mytheme/assets/svg/icon_name.svg
I want to put the url of my site in front of assets / svg / icon_name.svg
I already tried some tutorials, but I could not make any work.
Could someone please kind of help a noob in php?
i could make it work. So if someone have the same question, here is how i managed to get the code working.
<?php
// Note you must download the php files simple_html_dom.php from
// this link https://sourceforge.net/projects/simplehtmldom/files/
//than include them
include_once('simple_html_dom.php');
//target the website
$html = file_get_html('http://the_target_website.com');
//loop thru all images of the html dom
foreach($html ->find('img') as $item) {
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $item->src;
// Set a attribute
$item->src = 'http://yourwebsite.com/'.$value;
}
//save the variable
$html->save();
//findo on html the div you want to get the content
$elem = $html->find('div[id=container]', 0);
//output it using echo
echo $elem;
?>
That's it!
did you read the documentation for read and modify attributes
As per that
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;
// Set a attribute
$e->href = 'ursitename'.$value;

Edit iframe content using PHP, and preg_replace()

I need to load some 3rd party widget onto my website. The only way they distribute it is by means of clumsy old <iframe>.
I don't have much choice so what I do is get an iframe html code, using a proxy page on my website like so:
$iframe = file_get_contents('http://example.com/page_with_iframe_html.php');
Then I have to remove some specific parts in iframe like this:
$iframe = preg_replace('~<div class="someclass">[\s\S]*<\/div>~ix', '', $iframe);
In this way I intend to remove the unwanted section. And in the end i simply output the iframe like so:
echo ($iframe);
The iframe gets output alright, however the unwanted section is still there. The regex itself was tested using regex101, but it doesn't work.
You should try this way, Hope this will help you out. Here i am using sample HTML remove the div with given class name, First i load the document, query and remove that node from the child.
Try this code snippet here
<?php
ini_set('display_errors', 1);
//sample HTML content
$string1='<html>'
. '<body>'
. '<div>This is div 1</div>'
. '<div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div>'
. '</body>'
. '</html>';
$object= new DOMDocument();
$object->loadHTML($string1);
$xpathObj= new DOMXPath($object);
$result=$xpathObj->query('//div[#class="someclass"]');
foreach($result as $node)
{
$node->parentNode->removeChild($node);
}
echo $object->saveHTML();

Style -CSS a php session value

How to style php output?
Here is an code for example?
I can only style with css the code who is in the echo.How to style the session value?
echo "<h1>Logged in as:</h1>" .$_SESSION['myusername'];
Any tip?
You can use span tag to format text inside h1 tag :
echo "<h1 class='yourClass'>Logged in as:<span class='spanclass'>" .$_SESSION['myusername']. "</span></h1>"
CSS class (for example):
.spanclass{
color:green;
}
Use class to you html in php and add a style to your css file
In css page
.yourClass{
//add your style
}
In PHP
echo "<h1 class='yourClass'>Logged in as:</h1>" .$_SESSION['myusername'];

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'];
}
?>

Get SRC from div contents

I have code that gets a div contents:
include_once('simple_html_dom.php');
$html = file_get_html("link");
$ret = $html->find('div');
echo $ret[0];
preg_match_all('/(src)=("[^"]*")/i',$ret[0], $link);
echo $link[0];
It returns the full div contents including all the CSS. However I just wanted it to echo the information after src= basically just echoing the image link and nothing else. I've tried to use preg_match with no success.
Any ideas?
Your HTML parser will help you there - there should be a src property in the $ret object:
echo $ret[0]->src;
You don't need regexp for that since you already use a dom parser.
foreach($ret as $element)
echo $element->src,'<br/>';

Categories