Parsing title and link from html page - php

I wanna get the title and link from this html page :
<div class="gs_r">
<h3 class="gs_rt">
<span class="gs_ctc">[BOOK]</span>
titleA
</h3>
<div class="gs_ggs gs_fl">
<a href="http://exampleA.pdf" onmousedown="return scife_clk(this.href,'gga','gga','1')">
How can I get them?
here's the code :
<?php
include 'simple_html_dom.php';
$url = 'http://example.com';
$html = file_get_html($url);
//get the first link
foreach($html->find('span[class=gs_ctc]')as $Link){
echo $link;
}
foreach($html->find('div[class=gs_ggs gs_fl]')as $docLink){
echo $docLink;
}
?>

For the first link, it's a sibling of the <span>. Try this:
//get the first link
foreach($html->find('span[class=gs_ctc]') as $link){
$link = $link->next_sibling();
echo $link->plaintext;
echo $link->href;
}
As for the 2nd link, it's a child of the <div>:
foreach($html->find('div[class=gs_ggs gs_fl]') as $docLink){
$link = $docLink->first_child();
echo $link->href;
}
EDIT: The 2nd link is grouped with the first, so you can try this:
foreach($html->find('span[class=gs_ctc]') as $link){
foreach($link->parent()->parent()->find('div[class=gs_ggs gs_fl]') as $docLink){
$link1 = $link->next_sibling();
$link2 = $docLink->first_child();
if(preg_match('/\.pdf$/i', $link2->href) === 1){
echo $link1->plaintext;
echo $link1->href;
echo $link2->href;
}
}
}

Related

a href not clickable in an if statement

Hello could someone help me with something? I have this code:
<?php if($hashtagExist == true): ?>
<?php foreach($resultsTags as $tag): ?>
<?php echo $tag["Tags"];?>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?>
I'm testing out my function to print out hashtags, so if there's a hashtag it puts it in a tag. The results are correct, BUT the <a href> isn't a clickable link, but just plain text and I can't find the solution.
I prefer using php to echo html than to embed php into html tags or properties...
<?php
if($hashtagExist == true){
foreach($resultsTags as $tag){
$unhash = str_replace("#","",$tag); //Removes the hashes
echo ''.$unhash.''; //Display link with removed hashes
echo ''.$tag.''; //Retains the name of the hash with hash before the name i.e #hash
$out .= ''.$unhash.''; //in case you want to use this between some other tags
}
} else {
$out = '';
}
echo $out; //result variable can be used anywhere in the page
?>

Joomla: display page heading of menu item

I want to display the page heading of a Joomla menu item if this is filled. I've tryed with this code wothout success:
<h1 class="title">
<?php
if (null === ($this->params->get('page_heading')))
{
$mydoc = JFactory::getDocument();
$mytitle = $mydoc->getTitle();
echo $mytitle;
}
else
{
$active = JFactory::getApplication()->getMenu()->getActive();
echo $active->params->get('page_heading');
}
?>
</h1>
Any suggestion?
I've solved myself this way:
<h1 class="title">
<?php $menu = JFactory::getApplication()->getMenu();
$active = $menu->getActive();
$mydoc = JFactory::getDocument();
$mytitle = $mydoc->getTitle();
$pageHeading = $active->params->get('page_heading');
if($pageHeading != "")
{
echo $pageHeading;
}
else
{
echo $mytitle;
}
?>
</h1>
I had similar need and noticed this code doesn't work in Joomla 4.
Here is my solution:
Joomla 3
$menu = JFactory::getApplication()->getMenu()->getActive();
$title = $menu->title;
$page_heading = $menu->params->get('page_heading');
echo $page_heading ?? $title;
Joomla 4
$menu = JFactory::getApplication()->getMenu()->getActive();
$title = $menu->title;
$page_heading = $menu->getParams()->get('page_heading');
echo $page_heading ?? $title;
The only difference between these 2 is how you get the menu item params, params in J3 vs getParams() in J4.

How to pass an URL to a <a href="">

I'm still new to php and I've made a script that checks the language of the website and if it's English set's the button link to an english page and else to another page.
Here goes:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = "";
if($_SESSION["lang"]->lang!="ro"){
$url = $link2;
}
else {
$url = $link1;
}
?>
Button
It's supposed to be simple, I don't know why it doesn't work.
You'd echo the $url like so:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = "";
if ($_SESSION["lang"]->lang != "ro") {
$url = $link2;
}
else {
$url = $link1;
}
?>
Button
You can condense the code to this using the ternary operator:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = ($_SESSION["lang"]->lang != "ro") ? $link2 : $link1;
?>
Button
You have to echo $url in tag.
Button
Button
replace with:
Button
OR
Button
you can try this one:
See the page
<?php
$test = $_GET['p'];
?>
Test
or
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link

HTML Iframe get src with php

I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>

Html/Markup format using Picasa PHP FOREACH

Hi guys im trying to format an html output based on this picasa script using foreach, this way:
<? foreach($albums as $photo) {?>
<span><img src="<? echo $photo[1]; ?>" border=0></a><p><?=$photo[0]; ?></p></span>
<? } ?>
The output is:
<span><img src="foto1.jpg" border=0></a><p>This is pict 1 Album 1</p></span>
<span><img src="foto2.jpg" border=0></a><p>This is pict 2 Album 1</p></span>
<span><img src="foto3.jpg" border=0></a><p>This is pict 3 Album 1</p></span>
<span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span>
But i need this:
<div>
<h1>Album 1</h1>
<span><img src="foto1.jpg" border=0></a><p>This is pict 1 Album 1</p></span>
<span><img src="foto2.jpg" border=0></a><p>This is pict 2 Album 1</p></span>
<span><img src="foto3.jpg" border=0></a><p>This is pict 3 Album 1</p></span>
</div>
<div>
<h1>Album 2</h1>
<span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span>
</div>
The idea is bring all the album in my picasa account with their pictures inside, example:
album 1 has:
foto1.jpg
foto2.jpg
foto3.jpg
album 2 has:
foto4.jpg
So on ... That's it i hope someone could help me and understand better my really bad english :)
FULL SOURCE:
<?php
$userid = "cramosb"; // Your Google user name
$target = "PicasaBox.php/?album="; //URL to pass the name of the album to for the links
$imgmax = "512";
/*------------------------------------------------------------------------------
| USER CONFIGURATION END
------------------------------------------------------------------------------*/
// *** Only modify past this point if you know what you're doing ***
$insideentry = false;
$tag = "";
$title = "";
$url = "";
// function to parse the start of an XML element
function startElement($parser, $name, $attrs) {
global $insideentry, $tag, $title, $url;
if ($insideentry) {
$tag = $name;
if ($name == "MEDIA:CONTENT"){
$url = $attrs["URL"];
}
} elseif ($name == "ENTRY") {
$insideentry = true;
}
}
// function to parse the end of an XML element
function endElement($parser, $name) {
global $insideentry, $tag, $title, $url, $albums;
if ($name == "ENTRY") {
$albums[] = array($title, $url);
//echo $title . ' ' . $url;
$title = "";
$url = "";
$insideentry = false;
}
}
// function to parse the contents of an XML element
function characterData($parser, $data) {
global $insideentry, $tag, $title, $url;
if ($insideentry) {
if ($tag == "TITLE") {
$title .= $data;
}
}
}
// Lets get started...
// Create an XML parser, using the functions above
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
// The URL of the album feed I CHANGE THIS: $feed = "http://picasaweb.google.com/data/feed/api/user/" . $userid . "?kind=album"; TO:
$feed = "http://picasaweb.google.com/data/feed/api/user/" . $userid . "?kind=photo";
// Open the feed
$fp = fopen($feed,"r")
or die("Error reading RSS data.");
// Parse the feed
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
// Close the feed
fclose($fp);
xml_parser_free($xml_parser);
foreach($albums as $album)
{
$htmlout .= '<span><img src="' . $album[1] . '" border=0><p>' . $album[0] . '</p></span>';
}
print $htmlout;
exit;
?>
If you wish to split the output into 'albums' you need to know how to split the array of photos based on which album they should belong to. How are you populating $photo? Is each 'album' going to have only 3 photos in?
here, your homework done for you
<div>
<h1>Album 1</h1>
<? foreach($albums as $photo) {?>
<span><img src="<? echo $photo[1]; ?>" border=0></a><p><?=$photo[0]; ?></p></span>
<? } exit;?>
</div>
<div>
<h1>Album 2</h1>
<span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span>
</div>
note the other answer as well. splitting out which "foto" goes into which album requires more information, this is just an example with html markup so you can understand how the php is fitting in the mix

Categories