Can anyone please help me in finding out what have I done wrong in the code given below.
I have a PHP variable named news_content whose value is the following html...
<p><img src="./images/image1.jpeg" alt=""></p>
This value for variable news_content is obtained from a database query.
The function below creates a DOMDocument object using the variable news_content :
public function convert_to_tinymce_data($news_content)
{
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link)
{
$img_link=$link->getAttribute('src');
echo $link->getAttribute('src');
}
}
But nothing is being echoed (receiving a blank page).
I've tryed this code and it works:
$n = '<p><img src="./images/image1.jpeg" alt=""></p>';
function convert_to_tinymce_data($news_content){
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link) {
$img_link=$link->getAttribute('src');
echo $img_link;
}
}
convert_to_tinymce_data($n);
I see you have public function, but I don't see class. If it's global function, not a method of a class, that's the reason why it doesn't work. Or you don't call function properly.
(What you have there is function definition... it executes after you call it... Having same variable names in your function's definition and outside is bad practice and can be easily turn into problem)
Just to add: Check you're error_log file and let us know is there something interesting in it...
Related
i am getting some html code from database. It will be stored with few PHP defines. which are defined in a previous function where the current function will return the html code from database.
How can i get the define value inside the html code revived from database.
The defines you see has been declared in the data which i am getting from database. But i am unable to get their values. It is not dynamically getting the define value in $thankYouTemplate
function newPostPageMailDesign($title, $feturedImage, $smallContent, $permalink, $youMayAlsoLikeQuery) {
define('featuredImage', $feturedImage);
define('shortContent', $smallContent);
define('permalink', $permalink);
define('title', $title);
$thankYouTemplate = getData();
$HtmlAMiler = $thankYouTemplate;
return $HtmlAMiler;
}
function getData()
{
$Data = "Some HTML Code from database with".featuredImage.permalink."already in the databse";
return $Data;
}
Edit your HTML code in your database by adding a dummy string e.g "__DUMMYSTRING__". Now using str_replace replace the "__DUMMYSTRING__" with the content or HTML Code you wish to add.
example:
echo str_replace( '__DUMMYSTRING__' , $newHTMLCODE, $HTMLCodeFormDataBase);
May not be the efficient way but may work.
I'm fairly new to PHP, and i have a problem in defining a function that returns an array containing a price and description strings.
I am using the "simple html dom" php files that facilitates parsing.
The function i create requires 2 arguments : the link (from which it will grab data) and the id (used to get the proper css syntax).
This is the get_product_details.php
<?
require_once 'simple_html_dom.php';
$priceMatchTable=('span[id=our_price_display]');
$descMatchTable=('div[id=short_description_content]');
function get_prod_details( $link , $id ) {
global $priceMatchTable, $descMatchTable;
$html = file_get_html($link);
$result['price'] = $html->find($priceMatchTable[$id],0);
$result['desc'] = $html->find($descMatchTable[$id],0);
return $result;
}
And this is the main php:
<?php
include 'get_product_details.php';
$link = 'http://micromedia.tn/barette-memoire/1170-barette-m%C3%A9moire-1go-ddr-ii.html';
$id = 0;
$result = get_prod_details($link, $id);
echo $result['price'];
?>
Finally i get an error which tell:
find($priceMatchTable[$id],0); $result['desc'] = $html->find($descMatchTable[$id],0); return $result; }
Fatal error: Call to undefined function get_prod_details() in C:\xampp\htdocs\dom\index.php on line 8
Best regards!
This may sound silliy, but is
include 'get_product_details.php';
really pointing towards "get_product_details.php"?
Disable (//) the function call in you index.php and add a simple echo to your "get_product_details.php" to see if the file gets included.
I think you need something like:
include '/path/from/root_to_your/directory/get_product_details.php';
If your trying this in Windows land, it will look something like:
include 'C:\Documents\something\get_product_details.php';
Hello everyone i am fetching data by using simple html dom
This is my code of php which is fetching data from site
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html){
//check if language heading h2 exist then process forward
if($html->find('h2.channel-title',0)){
fetch data from tables
}
}
This line if($html->find('h2.channel-title',0)) finding h2.channel-title in find function of simple html dom give me a fatal error when h2.channer-title is not exist
In many pages <h2 class="channel-title"> English Links</h2> exists so i have code according to them and process further in my foreach loop it's working fine and fetched all data.
But
when <h2 class="channel-title">English Links</h2> tag is not exist it give me an error
Fatal error: Call to a member function find() on a non-object in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\autobot\engine\simple_html_dom.php on line 1113
Please help me i am stuck in it need help thank you. i want if h2.channel-title exist run my foreach code else run another but don't give an error its stop my whole script. :(
this might help.
$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html) {
$var = $html->find('h2.channel-title',0);
if(isset($var)) {
fetch data from tables
} else{
//do something
}
}
var_dump($html);
Which library you are using?
I am using this library (PHP Simple HTML DOM parser) to parse a link, here's the code:
function getSemanticRelevantKeywords($keyword){
$results = array();
$html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=");
foreach($html->find('span') as $e){
$results[] = $e->plaintext;
}
return $results;
}
but I am getting this error when I output the results:
Fatal error: Call to a member function find() on a non-object in
/var/www/vhosts/efamous.de/subdomains/sandbox/httpdocs/getNewTrusts.php
on line 25
(line 25 is the foreach loop), the odd thing is that it outputs everything (at least seemingly) correctly but I still get that error and can't figure out why.
The reason for this error is: the simple HTML DOM does not return the object if the size of the response from url is greater than 600000.
You can void it by changing the simple_html_dom.php file. Remove strlen($contents) > MAX_FILE_SIZE from the if condition of the file_get_html function.
This will solve your issue.
You just need to increase CONSTANT MAX_FILE_SIZE in file simple_html_dom.php.
For example:
define('MAX_FILE_SIZE', 999999999999999);
This error usually means that $html isn't an object.
It's odd that you say this seems to work. What happens if you output $html?
I'd imagine that the url isn't available and that $html is null.
Edit:
Looks like this may be an error in the parser. Someone has submitted a bug and added a check in his code as a workaround.
Before file_get_html/load_file method, you should first check if URL exists or not.
If the URL exists, you pass one step.
(Some servers, service a 404 page a valid HTML page. which has propriate HTML page structure like body, head, etc. But it has only text "This page couldn'!t find. 404 error bla bla..)
If URL is 200-OK, then you should check whether fetched thing is object and whether nodes are set.
That's the code i used in my pages.
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
// print_r($headers);
if (is_array($headers)){
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
$pageAddress='http://www.google.com';
if ( url_exists($pageAddress) ) {
$htmlPage->load_file( $pageAddress );
} else {
echo 'url doesn t exist, i stop';
return;
}
if( $htmlPage && is_object($htmlPage) && isset($htmlPage->nodes) )
{
// do your work here...
} else {
echo 'fetched page is not ok, i stop';
return;
}
For those arriving here via a search engine (as I did), after reading the info (and linked bug-report) above, I started some code-prodding and ended up fixing my problems with 2 extra checks after loading the dom;
$html = file_get_html('<your url here>');
// first check if $html->find exists
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
}
}
I'm having the same error come up in my logs and apart from the solutions mentioned above, it could also be that there is no 'span' in the document. I get the same error when searching for divs with a particular class that doesn't exist on the page, but when searching for something that I know exists on the page, the error doesn't pop up.
your script is OK.
I receive this error when it doase not find the element that i'm looking for on that page.
In your case, please check if the page that you are accessing it has 'SPAN' element
Simplest solution to this problem
if ($html = file_get_html("http://www.semager.de/api/keyword.php?q=". urlencode($keyword) ."&lang=de&out=html&count=2&threshold=") {
} else {
// do something else because couldn't find html
}
Error means, the find() function is either not defined yet or not available. Make sure you have loaded or include related function.
I am trying to implement a SAX based parser but somehow it only recognizes the start of the element and the end, the content is not provided in the logs. The variable holding the XML is filled correctly, I checked through a simple log.
Here is the code:
<?php
function startElementHandler($parser, $name, $attribs) {
if($name == "id"){
$id = TRUE;
}
}
function endElementHandler($parser,$name){
$id = FALSE;
}
function characterDataHandler($parser,$data){
if($id == TRUE){
echo $data;
}
}
global $id;
$id = FALSE;
$parser = xml_parser_create();
xml_set_element_handler($parser, "startElementHandler","endElementHandler");
xml_set_character_data_handler($parser,"characterDataHandler");
$xml = file_get_contents("http://itunes.apple.com/de/rss/topfreeapplications/limit=100/xml");
xml_parse($parser,$xml);
//xml_parser_free($parser);
?>
Any suggestion how I could recieve the content? Maybe I am missing something strange I am not aware of at the moment.
best regards
tim
Per your comment, $id never becomes true. Maybe you want attribs to have an id and not the name of the element. For example, if you have the XML
<div id="x"> blah </div>
You get
$name="div", $attribs={"id":"x"}
(this came out a bit of php-python, but i hope you get my point)
Is that really your bug?
According to http://www.phpcatalyst.com/php-compare-strings.php you should always compare strings using ===. Is that your bug?
You only used the xml_set_element_handler-callbacks. Those only:
Set up start and end element handlers
If you also want to retrieve the content of those tags, you'll also need to register the xml_set_character_data_handler-callback. Because this one:
Set up character data handler