Store html content into php variable? - php

I have some html in my php page that I fetched from database and will be used many times in my webpage, so I want to put it into a php variable for later use.
My sample code is
<div class="info">
Some content from database here...
<div class="more">
Some more text...
</div>
</div>
How to store this html into php variable?
Please also tell me how to echo content of that variable?

I do agree with #Jon Stirling but You can do like below:
$htmlData = '<div class="info">
Some content from database here...
<div class="more">
Some more text...
</div>
</div>';
and you can print this
echo $htmlData;

Related

PHP echo printing outside of my html bounds

I'm making a website where users can post stories etc and they get put into the MySQL database and the front page pulls the top-rated stories from the database and fills in the HTML template I made to display.
So I pull the top rated post and put it inside the article tags that I've made inside my HTML. The question is when it displays it displays outside of the box. Like so
The actual display is supposed to look like this
How do I format the PHP code, Or is there some better way to go about doing this?
<article class="post featured">
<header class="major">
<span class="date">April 25, 2017</span>
<h2>
And this is a<br />massive headline
</h2>
<?php echo '<p>' . htmlspecialchars($row['post'], ENT_QUOTES, 'UTF-8') . '</p>'; ?>
</header>
<img src="images/pic01.jpg" alt="" />
<ul class="actions special">
<li>Full Story</li>
</ul>
<span class="tags badge-pill badge-primary">Otero</span>
<span class="tags badge-pill badge-info">SCR</span>
<span class="tags badge-pill badge-success">Class of '22'</span>
<span class="tags badge-pill badge-danger">Male</span>
</article>
EDIT: Here is the HTML spot I'm talking about
Ok, It`s easy to solve. First your string has only one word (because doesn´t have spaces) an second, when you have a paragraph, the tag that wrap content has an autoalignement.
Maybe you can try adding some css rules for your parent div?
You may check this : https://stackoverflow.com/a/3367759/8186765

how to extract raw html code using simplehtmldom

I am trying to extract raw html from a web-page using simplehtmldom. I was wondering if it is possible using that library.
For example, let's say I have this web page I am trying to extract data from.
<div class="class1">
<div class="class2">
<div class="class3">
<p>p1</p>
<h1>header here!</h1>
<p>p2</p>
<img src="someimage"></img>
</div>
</div>
</div>
My goal is to extract everything within div class3 including the raw html code so when I get the data I can enter it to a text box which allows input for source code so it is formatted the same way it is from the webpage.
I have looked at simplehtmldom manuals and did some searching but have yet to find a solution.
Thank you.
Using your example html string
$html = str_get_html('<div class="class1">
<div class="class2">
<div class="class3">
<p>p1</p>
<h1>header here!</h1>
<p>p2</p>
<img src="someimage"></img>
</div>
</div>
</div>');
// Find all divs with class3
foreach($html->find('div[class=class3]') as $element) {
echo $element->outertext;
}

Getting div value (content/text) using XPath

I have next html structure:
<li id="REQUIRED_ITEM_1" class="listing-post">
<a class="listing-thumb" href="blah" title="blah" data-palette-listing-image="">
<img src="REQUIRED_ITEM_2" width="75" height="75" alt="blah"> </a>
<div class="listing-detail ">
<div class="listing-title">
<div class="listing-icon hidden"></div>
blah
<div class="listing-maker">
<span class="name wrap">blah</span>
</div>
</div>
<div class="listing-date">
REQUIRED_ITEM_6
</div>
<div class="listing-price">
Sold
</div>
</div>
</li>
There are few dozens of these <li> on the same page, all with different id and content. The content that I need is marked REQUIRED_ITEM_1 - REQUIRED_ITEM_6.
I am collecting the data from these <li>s with the help of Xpath.
Here is the code I use:
foreach($xpath->query("//li[#class='listing-post']") as $link) {
$REQUIRED_ITEM_1 = $link->getAttribute('id');
$REQUIRED_ITEM_2 = $xpath->query(".//img", $link)->item(0)->getAttribute('src');
$REQUIRED_ITEM_3 = $xpath->query(".//a", $link)->item(1)->getAttribute('href');
$REQUIRED_ITEM_4 = $xpath->query(".//a", $link)->item(1)->getAttribute('title');
$REQUIRED_ITEM_5 = $xpath->query(".//a", $link)->item(2)->getAttribute('href');
$REQUIRED_ITEM_6 = $xpath->query("./div/text", $link)->item(4);
}
It works as intended for the first 5 REQUIRED_ITEMs, however it seems the code to get text contained within listing-date div (REQUIRED_ITEM_6) is wrong.
Also, is this the best way to parse my html and collect data, or is there a better approach?
Here is the xPath to get REQUIRED_ITEM_6
//li[#class='listing-post']//div[#class='listing-date']/text()
That would be little bit faster (but first version may be more safe, since it is less dependent on XML structure).
//li[#class='listing-post']/div/div[#class='listing-date']/text()
So your code must look like something like this (but you may need to adjust it little bit with your php, not sure why you used item(4)).
$REQUIRED_ITEM_6 = $xpath->query(".//div[#class='listing-date']/text()", $link)->item(0)->textContent;

PHP Code Within Element Causes Repetition

I'm finding that placing php code snippets within my website's & tags is causing a problem with the code after it's placement. I first noticed it when placing avatars on my site in a list format where it works fine up to a point and then the same image is repeated for the rest of the list. Here is the code that I'm uisng for that:
<?php
$show_user .= "
<div class=\"section\">
<div class=\"sectionInner\">
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/$member_avatar\"></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_name</div><div class=\"searchInformationSecondary\"><i>"$member_summary"</i></div></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_subtype $member_type</div><div class=\"searchInformationSecondary\">$member_city, $member_county</div><div class=\"searchInformationThird\">View Details</div></div>
<div class=\"clearLeft\"></div>
</div>
</div>
<div class=\"searchResultSplitter\"></div>
";
?>
<?php echo $show_user; ?>
I'm now noticing it when placing php within my navigation bar where it's not closing the tag. Here is my code for the bar:
<div id="pageSubNavigation" class="page<?php echo $thispage; ?>SubNavigation">
Next Page
</div>
After this, all of the other tags show the same link. Any ideas why this might be?
You need to either jump out of the HTML to add the variables or wrap them in {} so they appear correctly.
Example:
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/".$member_avatar."\"></div>
or
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/{$member_avatar}\"></div>
Check out this article about string formatting with variables in PHP.

jquery find http elements in plain text and convert it to a link

I don't know if this is possible but I have a piece of plain text which always includes:
http://www.royalmail/portal/etc/etc/track.php?trackNumber=123345323
When dragged to my page from the database this obviously does not render as a link. Is there any way to select this piece of text (ie via the http://) and at the most basic wrap it with anchor tags - or more complexly - retrieve the address, wrap the address with anchor tags, and change the original http:// text to something more understandable such as 'Track Parcel'
It should be noted that there could be numerous tracking references looped out at once.
My containing HTML is as follows
<div class="message_holder">
<div class="mess_head" style="width:110px;">
<p><strong>Sender: </strong><? echo $row11['sender'];?></p>
</div>
<div class="mess_head" style="width:450px;">
<p><strong>Subject: </strong><span class="red"><? echo $row11['subject'];?></span></p>
</div>
<div class="mess_head" style="width:150px;">
<p><strong>Date Sent: </strong><? echo date('d/m/Y, H.ia', $row11['date_sent']);?></p>
</div>
<div class="mess_head" style="width:200px;">
<p><strong>Message ID: </strong><? echo $row11['message_id'];?></p>
</div>
<div class="mess_body">
<p><? echo html_entity_decode($row11['message']);?></p>
</div>
</div>
The plain text links will all show up in the 'mess_body' class. I have tried using html_entity_decode() but this did not do the trick. If there is an easy way to do this via PHP rather than JQuery it could be simpler.
I would do it with PHP:
$text = preg_replace(
'#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#',
'$1', /* or Your TEXT */
$text
);
If I guess right, the url can be in your message, so your code should go like this:
<?php
echo
preg_replace(
'#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#smi',
'$1',
html_entity_decode($row11['message'])
);
?>

Categories