Making links clickable w/o http - php

I made this website that uses XML file to store news entries. So basically a person will "submit" his name, date and content to the file and then I use DOM and for loop to print out the entries in homepage.
Anyway I'm planning to add WYSIWYG editor for the posting part but for now they're forced to use 'a' tags to make the links clickable. The problem is I noticed links that start with http:// works but links that start with www. doesn't. Is there a way to make both work using 'a' tags?
I'm still new to web development so I'm wondering if someone can help me.
$doc->load('entries.xml');
$newsArray = $doc->getElementsByTagName ('entry');
for($i = $newsArray->length; $i > 0; $i--)
{
$ent = $newsArray->item ($i-1);
$title = $ent->childNodes->item (1)->nodeValue;
$message = $ent->childNodes->item (2)->nodeValue;
$name = $ent->childNodes->item (3)->nodeValue;
$date = $ent->childNodes->item (4)->nodeValue;
if (get_magic_quotes_gpc()) {
$message1 = stripslashes($message);
}
else {
$message1 = $message;
}
echo "<div id='newsSec'>";
echo "<p></p>";
echo "<div class='newsTitle'> <b> $title </b> </div>";
echo "<div class='newsMessage'> " .nl2br($message1) ."</div>";
echo "<div class='newsName'> <b>Posted by:</b> $name <b>$date</b> </div>";
echo "</div>";

Append double-slashes to the beginning of the URL.
It will take the same protocol as the host-page itself
...
Reference: Protocol relative links: rfc1808

Add $message1 = "http://".$message1; before the echos. This will add the http:// in front of the link output.

Related

How to take an image from My SQL database and add it to a .html page

I have some images in a database which I would like to add to a html page
This is my current code
<div class = "gridrow">
<?php
foreach (range(1, 4) as $value) {
$result = $conn->query("select * from products where product_ID = '".$value."'");
$row = $result->fetch_array();
$name_p1 = $row['product'];
$price_p1 = $row['price'];
$image = "<img src='{$row['image']}'>";
echo "<div class = 'productwindow' >";
echo "<div class = 'productimage'><".$image."></div>";
echo "<div class = 'productvar'><p>".$name_p1."</p></div>";
echo "<div class = 'productvar'><p>$".$price_p1."</p></div>";
echo "</div>";
}
?>
</div>
This is what the page looks like
These are the errors I get
How can I make these images show correctly?
You're storing image data in the database, while the img src tag wants image URLs, that's why it's getting confused and you're getting errors.
The quick way around it is to convert the image data to base64 and pipe it in the src tag like so:
$image = '<img src="data:image/png;base64,'.base64_encode($row['image']).'">';
This is at best a hack, and not a great idea for a host of reasons, it also assumes all your images are PNG.

SQL Query PHP, populate content quickly

Not sure I worded the title correctly, I'm unsure of what this is called.
I have a while loop which is populated by an SQL query
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
echo '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_'.$Tchannel.'-320x180.jpg" alt="Thumbnail"><br />';
echo '<p>';
}
My problem is that when the page loads it does it in sections. I've seen other websites populate these straight away on page load. How would I replicate something like this?
See Example: http://puu.sh/oTh6v/73446c0c15.jpg
Could this be what you meant to achieve...? And by the way, the opening Paragraph Tag:< p > could contribute to some unexpected &/or sluggish behaviour since it was just opened without being closed anywhere in your code... I commented it out... You may try to see if it helps....
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
// WHY DO YOU NEED THIS OPENING PARAGRAPH TAG? WHERE IS IT CLOSED IN YOUR CODE?
//echo '<p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?
If you need the images wrapped in paragraph tags, you can do it differently:
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<p>'; // <== OPEN A PARAGRAPH
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
$strOutput .= '</p>'; // <== CLOSE THE PARAGRAPH
// OR ALL IN ONE LINE:
// $strOutput .= '<p><img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br /></p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?>

Finding within parsed div [Simple HTML DOM]

I want to fetch the <a> link within the div glance_details. I can't really make it work. Don't worry about including and the url and things, that is all correct.
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$html3->find('a',0)->outertext;
}
with
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$knaoss = $element3->plaintext;
echo $knaoss;
}
I can fetch the plain text content of the div, but what I want is the anchor (a) that will be within the div.
This is similar to what I receive in $knaoss if I remove the ->plaintext:
<div class="glance_details">
<a href="http://www.example.com/">
<img src="http://www.example.com/img.png">
</a>
"This is a description of the example"
</div>
Though all I want from it is:
http://www.example.com/
I must delete this answer because do not match the OP requirements after he has posted the requested HTML code
The solution was simple. Only had to change:
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$knaoss = $element3->plaintext;
}
to
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details > a') as $element3) {
$knaoss = $element3->href;
}
to find the href within div.glance_details. Problem was I used words like "url" and "link" instead of href, and could therefor not make it work.

Filter out link address from DOM results

I'm working with a DOM parser that grabs links from a website by the class thumbnail. This returns a list of links. They are then converted to their image state and shown on the page. The problem I'm having is I have 2 different links that are getting returned:
http://i.imgur.com/randomstuffhere
AND
http://imgur.com/randomstuffhere
I need to filter the results for the links that DO NOT contain the i.imgur.com. If the link is a imgur link but does not contain the i. before I need to filter it out not to show.
I have this so far and I cannot figure out where I've gone wrong... Any suggestions?
<?php
$html = file_get_contents('http://www.reddit.com/r/funny');
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hyperlinks = $xpath->evaluate('//a[#class="thumbnail "]');
foreach($hyperlinks as $hyperlink) {
if (preg_match("/http://imgur.com/", $hyperlink->getAttribute('href'))){
}
else{
echo "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlink->getAttribute('href') . "\" alt=\"\" />";
echo "<br />";
}
}
?>
You need to escape the // in http:// with \/\/.
You should probably use strpos, though.
if(strpos($hyperlink->getAttribute('href'), 'http://i.imgur.com/') !== FALSE){
echo "This is an i.imgur.com link!";
}

Is there something wrong with this XML/PHP Code for Tumblr to Website?

I am trying to link a tumblr feed to a website. I found this code (As you can see, something must be broken with it as it doesnt even format correctly in this post):
<?php
$request_url = “http://thewalkingtree.tumblr.com/api/read?type=post&start=0&num=1”;
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{‘regular-title’};
$post = $xml->posts->post->{‘regular-body’};
$link = $xml->posts->post[‘url’];
$small_post = substr($post,0,320);
echo ‘<h1>’.$title.’</h1>’;
echo ‘<p>’.$small_post.’</p>’;
echo “…”;
echo “</br><a target=frame2 href=’”.$link.”’>Read More</a>”;
?>
And i inserted the tumblr link that I will be using. When I try to preview my HTML, i get a bunch of messed up code that reads as follows:
posts->post->{'regular-title'}; $post = $xml->posts->post->{'regular-body'}; $link = $xml->posts->post['url']; $small_post = substr($post,0,320); echo '
'.$title.'
'; echo '
'.$small_post.'
'; echo "…"; echo "Read More"; ?>
Any help would be appreciated. Thank you!
That is PHP, not HTML. You need to process it with a PHP parser before delivering it to a web browser.
… it should also be rewritten so it can cache the remote data, and escape special characters before injecting the data into an HTML document.

Categories