Get specific output from HTML - php

<div class="info">
<ul class="links">
</ul>
<h1>Vykúpenie z väznice Shawshank</h1>
<ul class="names">
<li>
<img src="http://img.csfd.cz/assets/images/flags/flag_1.gif" alt="USA" />
<h3>Shawshank Redemption, The</h3>
</li>
<li>
<img src="http://img.csfd.cz/assets/images/flags/flag_34.gif" alt="CZ název" />
<h3>Vykoupení z věznice Shawshank</h3>
</li>
</ul>
Hey guys, this is part of big HTML, what i need is to use HTML SIMPLE DOM PRASER and get this 3 text : "Vyykúpenie z väznice Shawshank", "Shawshank Redemption, The", "Vykoupení z věznice Shawshank" How should i do that?
my try of PHP code:
$html = file_get_html('file.txt');
$ret = $html->find('ul[class="links"]'); //nazov filmu
foreach ($ret as $translate) {
$translate = $translate->innertext;
}
echo "$translate";

There are so many ways to do the translation. You can use contants you can use method. The one of my favourite way is to use Zend Translate check it.
The main aim is to create method which will change some contants depending on language.
class Translator
{
public static $lang = 'en';
public function translate($str){
if(isset($this->data[$str]))
return $this->data[$str];
else return 'no translation';
}
}
$translate->('DO_HOMEWORK');
Language should be stored in session and depends on language it should give proper values. You can get $this->data from CSV files, ini files or get it straightly from the array it doesn't really mater. You can use singleton pattern to Translator class.

I think you want so:
<div class="info">
<ul class="links">
<li><h1>Vykúpenie z väznice Shawshank</h1>
<ul class="names">
<li>
<img src="http://img.csfd.cz/assets/images/flags/flag_1.gif" alt="USA" />
<h3>Shawshank Redemption, The</h3>
</li>
<li>
<img src="http://img.csfd.cz/assets/images/flags/flag_34.gif" alt="CZ název" />
<h3>Vykoupení z věznice Shawshank</h3>
</li>
</ul>
</li>
</ul>
</div>

Related

how do I split this php file into MVC format?

i'm learning to program in OOP MVC,
I have this code for a simple nav menu:
<?php
$directory = "views";
$scannedDirectory = glob("$directory/*.php");
function uppercaseSpace($str) {
$re = '/(?=[A-Z][a-z])(?<!^)|(?=[A-Z])(?<=[a-z])/m';
$subst = ' ';
$result = preg_replace($re, $subst, $str);
return $result;
}
?>
<div id="header2">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" href="http://<?php echo $HOST ?>">Home</a>
</li>
<?php foreach ($scannedDirectory as $key => $value) {
$articleName = substr($value,6,-4);
printf('
<li class="nav-item">
<a class="nav-link active" href="?page=%s">%s</a>
</li>
',$articleName, ucfirst(uppercaseSpace($articleName)));
} ?>
</ul>
</nav>
</div> <!-- /header -->
I have a function in the php file so I could make a class navmenu.class and make a method of this function and put the frist 2 lines in the class as a property and the last html part becomes a view right?
but is this class a model or is it a controler?
here is a screenshot of my current file structure:
Is it worth to split this small code into MVC?
This file is now put in the subfolder includes because I am including the nav menu in my script.
Models are for database, View to render html, Controllers to handle a request
Beside that you can have helper classes and core classes
Follow some tutorials on how to create a mvc.
https://www.youtube.com/watch?v=OsCTzGASImQ&list=PLfdtiltiRHWGXVHXX09fxXDi-DqInchFD
https://www.youtube.com/watch?v=rkaLJrYnpOM&list=PLFPkAJFH7I0keB1qpWk5qVVUYdNLTEUs3
I followed the one of Curtis Parham he explained everything very well. After completing his tutorial I modified his framework alot by including composer, twig and changed the routing on how to handle variables and multiple languageses.

Read next html tag using PHP

This is my HTML part of code:
<ul>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li>
<h5>Price</h5>
<span>100$</span>
</li>
</ul>
In my php I am using php-simple-dom for finding tags. So php part looks something like this:
foreach($html->find("li") as $li)
{
if(strpos($li->plaintext,"<h5>Price</h5>") !== false)
{
var_dump($li->plaintext); // result: string("<h5>Price</h5><span>100$</span>")
}
}
I have some other idea:
foreach($html->find("h5") as $h5)
{
if(strpos($h5->plaintext,"Price") !== false)
{
// finding some way to read next tag
}
}
What I need ?
I need to get <span> value. This is example, in real code there are more tags and multiple spans in one <li>. But point is that next tag contain wanted information.
I'm not pretty sure how many tags could be in one <li>, but I belive <span> you are looking for is always after <h5>. You can use method $e->next_sibling() as follows:
foreach ($html->find('li h5') as $h5) {
$price = $h5->next_sibling();
echo $price->plaintext;
}
So you want to get a value of a specific tag, you could find DOMDocument::getElementsByTagName useful.
Return Values
A new DOMNodeList object containing all the matched elements.
Here is how you would use it:
$html = <<< HTML
<ul>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li> something,,,,... </li>
<li>
<h5>Price</h5>
<span>100$</span>
</li>
</ul>
HTML;
$dom = new DOMDocument;
$dom->loadXML($html);
$prices = $dom->getElementsByTagName('span');
foreach ($prices as $price) {
echo $price->nodeValue, PHP_EOL;
}
The above example will output: 100$
Go ahead and try it with several prices. It works as excepted.
You might also find the DOM documentation useful.

If query for tags in SPIP

It is possible to create an IF query for the tag in SPIP?
When i have only one article linked to the tag than he take me directly to the article. But if i have more than 1 article, than he does taken me to the tag page of spip, where are the listing of the linked article.
Here the code to go directly to the article
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<B_article>
<li>
<a<BOUCLE_article(ARTICLES){id_mot = #ID_MOT}{0, 1}> href="#URL_ARTICLE"</BOUCLE_article>>
#TITRE
</a>
</li>
</B_article>
</BOUCLE_mot>
</ul>
</div>
and here the code to go to the tag menu
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<li>[(#TITRE)]</li>
</BOUCLE_mot>
</ul>
</div>
Thanks in advance
Try this, will work with any SPIP version.
<BOUCLE_mot(MOTS){id_groupe ?}>
[(#REM) Link to mot]
<BOUCLE_check(ARTICLES) {id_mot} {1,1}>
<li>#_mot:TITRE</li>
[(#ID_ARTICLE|oui)]
</BOUCLE_check>
[(#REM) Link to one article]
<BOUCLE_one(ARTICLES) {id_mot} {0,1}>
<li>#_mot:TITRE</li>
</BOUCLE_one>
<//BOUCLE_check>
</BOUCLE_mot>
Conditional display is a real lack in SPIP. This is what I would do (don't have any SPIP environment with me to check).
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<BOUCLE_articles(ARTICLES){id_mot = #ID_MOT}>
<B_article_unique>
<li>
<a<BOUCLE_article_unique(ARTICLES){id_article}{si #TOTAL_BOUCLE|==1}> href="#URL_ARTICLE"</BOUCLE_article_multiple>>
#TITRE
</a>
</li>
</B_article_unique>
<BOUCLE_article_multiple(ARTICLES){id_article}{si #TOTAL_BOUCLE|!=1} />
<li>
<a href="#URL_MOT">
#TITRE
</a>
</li>
</B_article_muliple>
</BOUCLE_articles>
</BOUCLE_mot>
</ul>
</div>

Fatal error: Class 'Zend_Locale' not found

I'm developing a web app with Zend Framework 2.
I want to achieve urls with current locale automatically built-in, i.e. /locale/controller/action/etc. I wrote this:
<?php
$locale = new Zend_Locale();
?>
<ul class="nav">
<li>
Devices
<ul>
<li>
<img src="img/navbar/add.png" alt="+"> Add
</li>
</ul>
</li>
<li>
Favorites
<ul>
<li>
<img src="img/navbar/add.png" alt="+"> Add
</li>
</ul>
</li>
</ul>
and put it in a navbar.phtml file that i include in this way
include("navbar.phtml");
in my Application/view/layout/layout.phtml.
But, i only get this
Fatal error: Class 'Zend_Locale' not found in blabla\module\Application\view\layout\navbar.phtml on line 3
What's wrong? I forget some "use"? Sorry but i'm a newbie in php.
Installation of PHP intl extension is must.
Ok, i just resolved.
The code above is relative to the older version of Zend Framework.
In Zend Framework 2 the right code is this:
<?php
$translator = new Zend\I18n\Translator\Translator();
$locale = substr($translator->getLocale(), 0, 2);
?>
<ul class="nav">
<li>
Devices
<ul>
<li>
<img src="img/navbar/add.png" alt="+"> Add
</li>
</ul>
</li>
<li>
Favorites
<ul>
<li>
<img src="img/navbar/add.png" alt="+"> Add
</li>
</ul>
</li>
</ul>
And don't forget to enable intl PHP extension!

Wordpress Shortcode not working with WP Codex command and PHP

Below is a php issue I am having, it works in other ways but doesn't work the way I wish too.
Basically I wish to add the WP login and logout commands to the the A-HREF links, but the closing short code doesn't seem to be taken effect and I would like to know why and hopefully fix this problem.
<?php
echo do_shortcode('[not-level-visitors]') .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
do_shortcode('[/not-level-visitors]');
?>
Basically the above code is kind of working, but I just need the closing shortcode to work as it appears in my html and is not being recognised - I have created the above code from this below snippet that I am already using that works completely:
<?php
echo do_shortcode('[level-visitors]
<div id="SignUp">
<ul>
<li>
Sign Up
</li>
<li>
Login
</li>
</ul>
</div>
[/level-visitors]');
?>
The main difference I spotted is the single call vs multiple calls to do_shortcode, I assume it's failing to match the regex.
Maybe this will work:
echo do_shortcode('[not-level-visitors]
<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>[/not-level-visitors]');
It looks like you need to remove the extra opening/closing parentheses and extra do_shortcode call.
<?php
echo do_shortcode('[not-level-visitors]' .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
'[/not-level-visitors]');
?>

Categories