Why cheerio comments php (and other) tags when parse - cheerio

I need to show <?php parts of the string as inline html in my node app, but after cherrio.load string becomes to have html comment - <!--?php
const $ = cheerio.load('<?php echo "example"; ?>')
console.log($.html()) // <!--?php echo "example"; ?-->

Related

trying to extract data from txt file into and html

guys I'm doing a project game, and I'm having a struggle when I try to extract a text file content into an HTML I made.
currently, the game creates a score.txt file with the player score, and I'm trying to make an HTML that will show this score in the HTML automatically.
I will add my current HTML for reference so you could check it out:
This is the part of the code I'm struggling with
<h1><div id ="score">The score is <?p$ filename =
fopen('score.txt', 'r');
$file = fread($filename, filesize('score.txt'));
echo $file;
fclose($filename); </div> </h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">Good job traveler!</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?>
This should solve it
If in any case it doesn't work please refer to this url https://www.w3schools.com/php/php_file_open.asp

php xpath query: How to find all <a> in an HTML document that have a title attribute?

php xpath query: How to find all in an HTML document that have a title attribute?
For instance, the following is a sample of all the elements that should be matched. These <a> elements are also nested inside <div>:
<a href="/subdir/22222" title="The title">
Any help would be greatly appreciated.
The query is used in the following code:
$homepage = file_get_contents ("https://somedomain.com");
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
#$doc->loadHTML($homepage);
$xpath = new DOMXpath($doc);
$items = $xpath->query("//a[#title]");
foreach ($items as $node) {
$text = $xpath->evaluate("string(#title)",$node);
$href = $xpath->evaluate("string(#href)",$node);
echo $text;
echo "<br>";
echo $href;
}
//a[#title]
Selects all the a elements in document that have an attribute named title.

Yii2 Url::to returns local IP instead of public server name

I have an application developped using Yii Framework v2.
In one of the pages, images are included, but don't display properly as their url contains the local server IP address, and not the public server name. For example, I have img tag with src like https://10.0.0.20/path/to/image.jpg.
As I understand it (I'm not the one who developped this), the URL is set using the following function, from backend\modules\news\models\News.php:
public function getImageUrl()
{
$path = \Yii::getAlias('#uploads/news/') . $this->image;
if (!is_file($path)) {
return '';
} else {
return Url::to(\Yii::getAlias('#uploads/news-rel/' . $this->image), true);
}
}
It is called in a file frontend/widgets/news/views/_item.php with:
<?php
use kartik\helpers\Html;
use common\helpers\DateHelper;
use yii\helpers\StringHelper;
/* #var $this yii\web\View */
/* #var $model backend\modules\news\models\News */
setlocale(LC_TIME, 'fr_FR.UTF-8');
?>
<div class="news-item-image col-md-3">
<?php if ($model->image): ?>
<?= Html::img($model->getImageUrl()) ?>
<?php endif; ?>
</div>
<div class="news-item-content col-md-9">
<div class="news-item-content-title"><?= $model->title ?></div>
<div class="news-item-content-date">
<?= strftime('%e %B %G', strtotime($model->publishDate)) ?>
</div>
<div class="news-item-content-body"><?= StringHelper::truncateWords(strip_tags($model->body), 20) ?></div>
<?= Html::a(Yii::t('app', 'Read more'), ['/news/view', 'id' => $model->id], ['class' => 'news-item-content-read-more']) ?>
</div>
In other places, same kind of code is used, and return a proper url (https://www.example.com/path/to/resource).
What should I look after to debug this ?
EDIT1: #uploads alias is defined in backend/config/bootstrap.php with this lines:
/*Yii::setAlias('#uploads', realpath(dirname(__FILE__).'/../web') . '/uploads');
Yii::setAlias('uploads/images-rel', '/uploads/images');
Yii::setAlias('uploads/editor-rel', '/uploads/editor');*/
It is the same alias that is used in many other places, and returns urls with server name.
Thanks
Franck
check your config/web.php file.
Look for the hostInfo
Or set it up yourself. It's going to alter your Urls.
Also:
When $scheme is specified (either a string or true), an absolute URL with host info (obtained from yii\web\UrlManager::$hostInfo) will be returned. If $url is already an absolute URL, its scheme will be replaced with the specified one.
Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-url

removing unused p tags

I have the following code which puts < p > tags back on the paragraphs in my Wordpress post. (I removed the images and placed them later, uing this to put just the text)
<p>
<?php
$content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
</p>
It works, and puts my paragraphs in p tags again, However it outputs empty < p > < /p > tags in between my actual paragraphs. I want to get rid of these empty paragraph tags
You're just overwriting the variable
Try this (notice .= instead of =) .= adds to the variable as opposed to overwriting it.
$content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
$content .= wpautop($content); // Add paragraph-tags
$content .= str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
Use this function it will help you to remove this <p></p> tag
<?php echo get_the_content();?>

EntityMalformedException when retrieving Drupal 7 custom field

I am playing with Drupal and I am trying to add a second line to the site slogan.
The following is the piece of page.tpl.php where I am working.
<?php if ($site_name || $site_slogan): ?>
<div id="name-and-slogan" class="hgroup">
<?php if ($site_name): ?>
<h1 class="site-name">
<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>">
<?php print $site_name; ?>
</a>
</h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<p class="site-slogan"><?php print $site_slogan; ?></p>
<?php endif; ?>
<?php
/* ADDED */
$node = menu_get_object();
$siteslogan2 = field_get_items('node', $node, 'field_siteslogan2');
?>
<?php if ($siteslogan2): ?>
<p class="site-slogan2"><?php print $siteslogan2; ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
I basically added a new Content Type with a field called siteslogan2 (field_siteslogan2) and now I would like to retrieve and show it here.
The first problem is that the $node var is not defined (even if according to the documentation it should be). The second problem is that I receive EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() when I define it manually and run it.
First of all, is this the right approach to the problem? Secondly, why do I receive the EntityMalformedException and how can I fix it?
var_dump($node) produces NULL. It must be the way I get the $node content that is not good. The doc is a bit cryptic to me when it says:
$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).
This sort of thing is can be done using a preprocessing function in your theme's template.php to define and set a variable in the $varables array.
This variable then becomes available to whichever template ( page, node, form, etc ) you have done the preprocessing on. Your theme's template.php file will most likely have comments on how to do this.
for instance, doing this in the template.php creates or modifies the value of the variable $display_header, making it available for use in node.tpl.php
function yourthemename_preprocess_node(&$variables, $hook) {
$variables['display_header'] = false;
}
You can then modify the node template file to use this variable.
For something simple like a sub-slogan, you can add a setting to the theme, so it shows up on the theme's configuration page like any other theme setting.
This requires implementing this function in your theme's theme-settings.php:
function yourthemename_form_system_theme_settings_alter(&$form, $form_state) {}
This provides information to the settings form regarding the new setting. like this:
https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_form_system_theme_settings_alter/7
Hopefully that's enough to get you started.