DOMXPath/DOM - Check if can find the element in the DOM - dom

Here is my code:
$url = "http://www.sportsdirect.com/football-shirts/premier-league-football-shirts/arsenal-football-shirts";
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$n = $xpath->query('//div[#class="s-producttext-top-wrapper"]');
$l = $xpath->query('//div[#class="s-producttext-top-wrapper"]/a');
$p = $xpath->query('//div[#class="s-largered"]');
$im = $xpath->query('//a[#class="ProductImageList"]');
$ra = $xpath->query('//div[#class="s-producttext-review"]/div');
$nl = $xpath->query('//a[#class="swipeNumberClick"][last()]')->item(0)->C14N();
echo $nl;
How can i check if $nl finds the element in the DOM ?
Thanks in advance!

As XPath:query() returns an instance of DOMNodeList, you can check length attribute in order check if any node was found:
$nl = $xpath->query('//a[#class="swipeNumberClick"][last()]');
# or $nl->length > 0
if ($nl->length) {
$nl = $nl->item(0)->C14N();
}

Related

cakephp3 plugin not load my translation file

my cakephp3 plugin with name "AdminTheme" has a localisation file.
AdminTheme
/src
/Locale
/de
AdminTheme.po
The locale change perfectly. In my templates I use the function
__d('AdminTheme', 'Sign In')
The function
\Cake\I18n\MessagesFileLoader::__invoke()
says in line 122 (is_file()): File not found.
pathToMyCake/plugins/AdminTheme/src/Locale/de/AdminTheme.po
public function __invoke()
{
$package = new Package('default');
$folders = $this->translationsFolders();
$ext = $this->_extension;
$file = false;
$fileName = $this->_name;
$pos = strpos($fileName, '/');
if ($pos !== false) {
$fileName = substr($fileName, $pos + 1);
}
foreach ($folders as $folder) {
$path = $folder . $fileName . ".$ext";
if (is_file($path)) {
$file = $path;
break;
}
}
if (!$file) {
return $package;
}
$name = ucfirst($ext);
$class = App::classname($name, 'I18n\Parser', 'FileParser');
if (!$class) {
throw new RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
}
$messages = (new $class)->parse($file);
$package->setMessages($messages);
return $package;
}
Why?
The file is correctly?
Please help me. Very thank's.
The debugger screens
I'm confused.

Perl variables scoped within a sub

How would I scope to this variable depending on a conditional of papertype?
I have tried it several ways and I am getting errors and I am befuddled.
sub paperdisplay_getPaperLink {
my ( $self, $args ) = #_;
my $paper = $args->{paper};
my $linktext = $args->{linktext};
my $session = $args->{session};
my $query = $self->request;
my $password = $query->param('password');
if ( $paper->{Type} eq 'Break' ) {
my $url = $something;
} else {
my $url = $somethingelse;
}
my $link = qq(<a title="$linktext" target="other" href="$url">$linktext</a>);
return $link;
}
You have to declare it in the block you want to use it in. If you declare it inside the if or else block, it'll only exist there. The variable would be destroyed when the block ends.
my $url;
if ($paper->{Type} eq 'Break') {
$url = $something
} else {
$url = $somethingelse
}
# $url still exists down here
Use the Conditional Operator to initialize a variable without the need for an if block:
my $url = $paper->{Type} eq 'Break'
? $something
: $somethingelse;

How to print the outputs on excel file to each columns by using perl?

How to write the output in each rows and each columns?
use Win32::OLE;
#a = (1..5);
#b = (20..30);
#c = (30..48);
#array = ("#a","#b","#c");
my $xl = Win32::OLE->new('Excel.Application');
$xl->{EnableEvents} = 0;
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Add;
my $sht = $wb->Sheets(1);
$sht->{Name} = "Occurance";
foreach $s (#array){
#each = split(' ',$s);
$col++;
foreach (#each){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
In this script gives output, But i expect the array value of "#b" write to the column B, Row 1 to end of array. Then "#c" write to the column c, Row 1 to end of array. How can do it?
You're not initializing $row in the column loop.
What you have is:
foreach $s (#array){
$col++;
foreach (#$s){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
Add 'my $row = 0;'
foreach $s (#array){
$col++;
my $row = 0;
foreach (#$s){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
For a little more advanced perl, you can make #array an array of arrays instead of an array of strings:
use strict;
use warnings;
use Win32::OLE;
my #a = (1..5);
my #b = (20..30);
my #c = (30..48);
my #array = (\#a,\#b,\#c);
my $xl = Win32::OLE->new('Excel.Application');
$xl->{EnableEvents} = 0;
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Add;
my $sht = $wb->Sheets(1);
$sht->{Name} = "Occurance";
my $col = 0;
foreach my $s (#array){
$col++;
my $row = 0;
foreach my $item (#$s){
$row++;
$sht->cells($row,$col)->{Value} = $item;
}
}

Date formatting issue - PHP

I have an array :
$multiple_dates[0] = 2013-06-09 00:00:00;
$multiple_dates[1] = 2014-06-13 12:23:00;
Here is my code :
$format = "d-m-Y h:m:s";
for ($i = 0; $i < count($multiple_dates); $i++) {
$date_display = date_format(date_create($multiple_dates[$i]), $format);
$fdate = date_translate($format, $date_display);
echo $fdate;
}
But output is :
09-06-2013 12:06:0013-06-2014 12:06:00
Time is not correct...Any idea ?
That's because minutes fraction is i, not m (which is month)
So
$format = "Y-m-d H:i:s";
Your format is invalid. Try with:
$format = "d-m-Y h:i:s";
It should be "i" like this
$format = "d-m-Y h:i:s";
edit like this you can get it
Correct format is
$format = "d-m-Y H:i:s";
Check this code --
<?php
$mydate = '2014-06-13 12:23:00';
$format = "d-m-Y h:i:s";
echo $date_display = date_format(date_create($mydate), $format);
go for $timestamp = strtotime("09-06-2013 12:06:00") and set on date('d-m-Y h:m:s', $timestamp);
for ($i = 0; $i < count($multiple_dates); $i++) {
$timestamp = strtotime($multiple_dates[$i]));
$fdate=date('d-m-Y h:m:s', $timestamp);
echo $fdate;
}
You can just use strtotime. E.g.:
foreach ($multiple_dates as $date) {
echo date($format, strtotime($date));
}
tryin this one is easier....
for ($i = 0; $i < count($multiple_dates); $i++) {
echo date('d-m-Y H:i:s',strtotime($multiple_dates[$i]));
}
i would prefer this

How to get the name from content-disposition in MIME::Entity part?

my $Parser = new MIME::Parser;
my $entity = $Parser->parse_data( $body );
my #parts = $entity->parts;
for $part(#parts){
my $type=$part->mime_type;
my $bhandle=$part->bodyhandle;
$header = $part->head();
$content_disp = $header->get('Content-Disposition');
if ($type =~ /text/i){
$bodydata = "";
if (my $io = $part->open("r")) {
while (defined($_ = $io->getline)) {
$bodydata .= $_;
}
$io->close;
print $bodydata;
}
}
}
I think you're looking for the recommended_filename method:
$header = $part->head();
$filename = $header->recommended_filename;
Be sure to check the return value for sanity. Note that it can also be undef.