php class returns result with enter sign - php

so i have this code that gets an aray of rows from database, in order to put it in a
class dat{
public $result;
public function loadParteneri (){
global $db;
$nrcrt = 1;
$db->orderBy("numefirma","asc");
$parteneri = $db->get ("parteneri");
if ($db->count > 0)
foreach ($parteneri as $partener) {
$this->result[] = <<<EOD
<div id="{$partener['id']}" class="row lines">
<div class="col-md-1">{$nrcrt}</div>
<div class="col-md-2 serie">{$partener['numefirma']}</div>
<div class="col-md-1">{$partener['tippartener']}</div>
<div class="col-md-2">{$partener['cif']}</div>
<div class="col-md-2">{$partener['oras']}</div>
<div class="col-md-2">{$partener['nume']} {$partener['prenume']}</div>
<div class="col-md-2">
<span class="glyphicon glyphicon-search view-partener" title="Vizualizare"></span>
<span class="glyphicon glyphicon-trash trash-partener" title="Stergere"></span>
</div>
</div>
EOD;
$nrcrt++;
}
$db->disconnect();
}
}
than i call the class like this and send the response as json to ajax:
$response = new dat;
$response -> loadParteneri();
$response->result;
exit(json_encode($response));
problem is the response looks like this:
"<div id="97" class="row lines">
↵ <div class="col-md-1">1</div>
↵ <div class="col-md-2 serie">2 BRUNO MARS</div>
↵ <div class="col-md-1">clientfinal</div>
↵ <div class="col-md-2">RO15165473</div>
↵ <div class="col-md-2">HUNEDOARA</div>
↵ <div class="col-md-2"> </div>
↵ <div class="col-md-2">
↵ <span class="glyphicon glyphicon-search view-partener" title="Vizualizare"></span>
↵ <span class="glyphicon glyphicon-trash trash-partener" title="Stergere"></span>
↵ </div>
↵</div>"
what am i doing wrong? thanks in advance!

You could try to use str_replace:
$result = << EOD
// DIV's etc...
EOD;
$this->result[] = str_replace(["\n", "\r"], "", $result);
It is a bit hackish and replaces ALL the newlines in the result (Also between the DIV's).
You may want to consider using templates to separate you HTML and code instead of mixing it.

Related

How can specific sequentially elements be wrapped with a single container using DOMDocument?

Let's say I've got the following markup:
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Books, Literature and Languages
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Business and Consumer Information
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
<p>Some text in between</p>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Fine Arts and Music
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Genealogy
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
I want to wrap each group of .handorgel__* elements so that they are contained with a container <div class="handorgel">.
<div class="handorgel">
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Books, Literature and Languages
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Business and Consumer Information
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
</div>
<p>Some text in between</p>
<div class="handorgel">
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Fine Arts and Music
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Genealogy
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
</div>
There could be any number of elements within each group, and any number of groups of a page. How can I detect these groups and wrap them appropriately? I currently use DOMDocument for a number of things on this project, so if possible, I'd like to use that for this purpose as well, unless there's a clearly superior method.
Managed to get this working myself, after a bunch of trial-and-error. Wasn't quite as difficult as I assumed it would be, DOMDocument actually takes care of some of the removal logic itself.
/**
* Wrap handorgel groups in appropriate containers
*
* #param string $content
*
* #return string
*/
function gpl_wrap_handorgel_shortcodes(string $content): string {
if (! is_admin() && $content) {
$DOM = new DOMDocument();
// disable errors to get around HTML5 warnings...
libxml_use_internal_errors(true);
// load in content
$DOM->loadHTML(mb_convert_encoding("<html><body>{$content}</body></html>", "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);
// reset errors to get around HTML5 warnings...
libxml_clear_errors();
$body = $DOM->getElementsByTagName("body");
$handorgels = [];
$prev_class = "";
foreach ($body[0]->childNodes as $element) {
/**
* Ensure that only HTML nodes get checked/modified
*/
if ($element->nodeType == 1) {
$current_class = $element->getAttribute("class");
/**
* Find any handorgel elements
*/
if (preg_match("/handorgel__/", $current_class)) {
$group = array_key_last($handorgels);
/**
* If the previous class didn't include `handorgel__`, create a new handorgel object
*/
if (! preg_match("/handorgel__/", $prev_class)) {
$handorgels[] = [
"container" => $DOM->createElement("div"),
"elements" => [],
];
/**
* Update `$group` to match the new container
*/
$group = array_key_last($handorgels);
}
/**
* Append the current element to the group to be moved after all sequential handorgel
* elements are located for its group
*/
$handorgels[$group]["elements"][] = $element;
}
/**
* Update `$prev_class` to track where handorgel groups should begin and end
*/
$prev_class = $current_class;
}
}
/**
* Construct the grouped handorgels
*/
if ($handorgels) {
foreach ($handorgels as $group => $handorgel) {
$handorgel["container"]->setAttribute("class", "handorgel");
foreach ($handorgel["elements"] as $key => $element) {
/**
* Insert the container in the starting position for the group
*/
if ($key === 0) {
$element->parentNode->insertBefore($handorgels[$group]["container"], $element);
}
$handorgel["container"]->appendChild($element);
}
}
}
/**
* Remove unneeded tags (inserted for parsing reasons)
*/
$content = remove_extra_tags($DOM); // custom function that removes html/body and outputs the DOMDocument as a string
}
return $content;
}
add_filter("the_content", "gpl_wrap_handorgel_shortcodes", 30, 1);

Include php is throwing result in header

I am trying to use the php code to display a comment box on a page:
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
For some reason the comment box is not appearing where i want it to appear,it keeps throwing the comment box to header, is there any way i can make it appear exactly where i want it to?, here is where i am trying to use it(not a complete code):
<!--<h1 align="center" style="font-size:1.2em"><strong>'.$row['title'].'</strong></h1>-->
<div class="song-art"><img src="'.$this->url.'/thumb.php?src='.$row['art'].'&t=m&w=112&h=112" id="song-art'.$row['id'].'" title="'.$row['title'].'" alt="'.$row['title'].'"/></div>
<div class="song-top">
<div class="song-timeago">
<a href="'.$this->url.'/mp3download/'.$row['id'].'/'.$this->genSlug($row['title']).'.html"><span id="time'.$row['id'].'">
<div class="timeago'.$b.'" title="'.$time.'">
'.$time.'
</div>
</span>
</a>
</div>
<div data-track-name="'.$row['name'].'" data-track-id="'.$row['id'].'" id="play'.$row['id'].'" class="track song-play-btn">
</div>
<div class="song-titles">
<div class="song-author"><a onmouseover="profileCard('.$row['idu'].', '.$row['id'].', 0, 0);" onmouseout="profileCard(0, 0, 0, 1);" onclick="profileCard(0, 0, 1, 1);" href="'.$this->url.'/index.php?a=profile&u='.$row['username'].'">'.realName($row['username'], $row['first_name'], $row['last_name']).'</a></div>
<div class="song-tag">
'.$tag.'
</div>
<div class="song-title">
<div class="track-link"><h2 style="font-size:1.0em">
<!--<div id="song-name'.$row['id'].'">'.$row['title'].'</div>-->
</h2></div>
</div>
</div>
</div>
<div class="player-controls">
<div id="song-controls'.$row['id'].'">
<div id="jp_container_123" class="jp-audio">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="track-actions-container">
</div>
'.$extra_links.'
'.$seo_links.'
'.$comment.'
'.$charts.'
</div>';
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
$start = (isset($row['extra_id'])) ? $row['extra_id'] : $row['id'];
}
}
I am trying to display it right after '.$charts.' but it keeps showing up in header
it's looking like you haven't worked to match HTML in index and loadComments.php .
first copy your loadComments.php code in place of include('commentanything/php/loadComments.php');
and check for html/css errors. It's there.
And if it failed too, your php/html nesting is wrong which i cant tell because u don't have even used to know us how and where u are stuffing php. You simply added variables in above displayed code

Find and replace a piece of code in HTML

I have a template with predefined blocks. It looks like below:
<body>
<div class="row">
<div class="col-lg-6">
<include name="position1" type="module">
</div>
<div class="col-lg-6">
<include name="position2" type="module">
</div>
</div>
<div class="row">
<div class="col-lg-10">
<include type="content">
</div>
<div class="col-lg-2">
<include name="right" type="module">
</div>
</div>
</body>
My predefined blocks:
-position1
-position2
-right
I'm looking for the way to get the blocks above as array from my page;
And small function to replace block with another content by block name;
For example:
function replace_block('position1', '<b>Replaced content</b>') {
... code
And as output:
...
<div class="col-lg-6">
<b>Replaced content</b>
</div>
...
}
Thanks!
Try this:
<?php
echo replaceBlock('position1', 'this is a replacement string', $html);
function replaceBlock($name,$replacement,$html) {
$pattern = '/(<include name="' . $name . '".*?>)/';
if (preg_match($pattern, $html, $matches)) {
$html = str_replace($matches[1],$replacement,$html);
}
return $html;
}

str_replace work incorrect ("str_replace" makes changes to the $replace parameter)

Good day!
Specify, for whatever reason, if the number is greater than 10, then str_replace() makes changes to the $replace parameter, cutting units and leaving only dozens?
Input data ($data):
...
<div onclick="window.location.href='/template-04.php?type=users&char=7';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=8';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=9';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=10';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=11';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=12';"></div>
...
very simple PHP code:
for($axx = 0; $axx < 68; $axx ++)
{
$z = '['.$axx.']';
$newName = 'templ4-user-'.$z.'.html?'.$z;
echo '<br>'.$newName; // echo (axx = 13): <br>templ4-user-[13].html?[13]
$data = str_replace('template-04.php?type=users&char='.$axx, $newName, $data);
}
Result $data incorrect. (if $axx > 10) Why?
...
<div onclick="window.location.href='/templ4-user-[7].html?[7]';"></div>
<div onclick="window.location.href='/templ4-user-[8].html?[8]';"></div>
<div onclick="window.location.href='/templ4-user-[9].html?[9]';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]0';"></div> <------ !!!!!!!
<div onclick="window.location.href='/templ4-user-[1].html?[1]1';"></div> <------ !!!!!!!
<div onclick="window.location.href='/templ4-user-[1].html?[1]2';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]3';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]4';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]5';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]6';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]7';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]8';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]9';"></div>
<div onclick="window.location.href='/templ4-user-[2].html?[2]0';"></div>
...
Please help.
It is because in first iteration all 1's will become [1]'s which means that 12 will become [1]2 and will never match agains 12 anymore.
Instead of loops, you could use preg_replace :
$data = <<<EOS
<div onclick="window.location.href='/template-04.php?type=users&char=7';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=8';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=9';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=10';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=11';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=12';"></div>
EOS;
$pattern = '/template-04.php\?type=users&char=(\d+)/i';
$replacement = 'templ4-user-[$1].html?[$1]';
echo preg_replace($pattern, $replacement, $data);
Result:
<div onclick="window.location.href='/templ4-user-[7].html?[7]';"></div>
<div onclick="window.location.href='/templ4-user-[8].html?[8]';"></div>
<div onclick="window.location.href='/templ4-user-[9].html?[9]';"></div>
<div onclick="window.location.href='/templ4-user-[10].html?[10]';"></div>
<div onclick="window.location.href='/templ4-user-[11].html?[11]';"></div>
<div onclick="window.location.href='/templ4-user-[12].html?[12]';"></div>

DOMXpath query on query

I have the following HTML:
[...]
<div class="row clearfix">
<div class="col1">Data</div>
<div class="col2">Data</div>
<div class="col3">Data</div>
<div class="col4">Data</div>
<div class="col5">Data</div>
<div class="col6">Data</div>
<div class="col7">Data</div>
<div class="col8">Data</div>
</div><!--// row-->
<div class="row clearfix otherClass">
<div class="col1">Data</div>
<div class="col2">Data</div>
<div class="col3">Data</div>
<div class="col4">Data</div>
<div class="col5">Data</div>
<div class="col6">Data</div>
<div class="col7">Data</div>
<div class="col8">Data</div>
</div><!--// row-->
<div class="row clearfix thirdClass">
<div class="col1">Data</div>
<div class="col2">Data</div>
<div class="col3">Data</div>
<div class="col4">Data</div>
<div class="col5">Data</div>
<div class="col6">Data</div>
<div class="col7">Data</div>
<div class="col8">Data</div>
</div><!--// row-->
[...]
I want to get all of these divs out of the HTML, they all start with "row clearfix" as class, but can have more data to it.
After that I want to be able to handle each col separetely, so get the value of col1, col2, col3 ect.
I have written this code, but am stuck now. Can someone help me out?
$oDom = new DOMDocument();
$oDom->loadHtml($a_sHTML);
$oDomXpath = new DOMXpath($oDom);
$oDomObject = $oDomXpath->query('//div[#class="row clearfix"]');
foreach ($oDomObject as $oObject) {
var_dump($oObject->query('//div[#class="col1"]')->nodeValue);
}
UPDATE *Solution*
Thanks to the replies below, I got it working with the following code:
$oDom = new DOMDocument();
#$oDom->loadHtml($a_sHTML);
$oDomXpath = new DOMXpath($oDom);
$oDomObject = $oDomXpath->query('//div[contains(#class,"row") and contains(#class,"clearfix")]');
foreach ($oDomObject as $oObject) {
foreach($oObject->childNodes as $col)
{
if ($col->hasAttributes())
{
var_dump($col->getAttribute('class') . " == " . trim($col->nodeValue));
}
}
}
To match the outer divs I think that what you need is
//div[starts-with(#class,"row clearfix")]
or
//div[contains(#class,"row clearfix")]
or
//div[contains(#class,"row") and contains(#class,"clearfix")]
I'd go for the last one because the class names could be in any order.
I am not 100% sure what you want to do with the inner div, but you could get them with something like this:
div[starts-with(#class,"col")]

Categories