Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Use PHP parsing to write out contents between tags

Posted on 2010-08-17
5
240 Views
Last Modified: 2012-05-10
I'm trying to "echo" the contents inside my "<span>" tag to the page using the getArticles function below.   It doesn't seem to want to write it out.    The function and only part of the HTML are below.   What am I doing wrong?


// HTML
<span class='clr2 b'>My Test</span>
**************************************************************
// FUNCTION
function getArticles($page) {  

    global $articles;  
 
    $html = new simple_html_dom();  
    $html->load_file($page);  
      //echo $html;
      
      $items = $html->find("span[class=clr2 b]");  

      foreach($items as $post) {

            # remember comments count as nodes
            // outertext
            // innertext
            // plaintext
            $articles[] = array($post->children(0)->outertext);

                        
      }
      
    foreach($articles as $item) {
        echo $item[0];

    }      
  }
 
*************************************************
0
Comment
Question by:saturation
  • 2
  • 2
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33457722
I'm not sure why you are doing all of that.  I just put it in the page where I need it.
<span class='clr2 b'><?php echo $article; ?></span>

Open in new window

0
 

Author Comment

by:saturation
ID: 33457750
It's because I don't have direct access to the page, and I'm just trying to get used to handling a parser, so I don't have a choice--the data is already written out to the page.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33457880
What are you getting on the screen right now?
0
 

Author Comment

by:saturation
ID: 33458028
Nothing....Here's the full code:

// TEST.HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<span class='clr2 b'>My Test</span>
</body>
</html>


// INDEX.HTML
include('simple_html_dom.php');

$articles = array();
getArticles('test.html');


// SIMPLE_HTML_DOM.PHP -- THIS PAGE DOES WORK, I DIDN'T INCLUDE ALL OF THE CODE.   I
function getArticlesCars($page) {  

    global $articles;  
 
    $html = new simple_html_dom();  
    $html->load_file($page);  
      
      $items = $html->find("span[class=clr2 b]");  

      foreach($items as $post) {

            # remember comments count as nodes
            // outertext
            // innertext
            // plaintext
            $articles[] = array($post->children(0)->outertext);            
      }
      
    foreach($articles as $item) {
        echo $item[0];
    }      
  }
0
 
LVL 8

Accepted Solution

by:
ropenner earned 500 total points
ID: 33460103
This works to printout as output

My Test
<?PHP
include('simple_html_dom.php');

$articles = array();
getArticles('test.html');

function getArticles($page) {   

    global $articles;   
  
    $html = new simple_html_dom();   
    $html->load_file($page);   
      
      $items = $html->find("span[class=clr2 b]");  

      foreach($items as $post) {

            # remember comments count as nodes
            // outertext
            // innertext
            // plaintext
			$articles[] = array($post->text());            
//            $articles[] = array($post->children(0)->outertext);            
      }
      
    foreach($articles as $item) {
        echo $item[0];
    }      
  }
?>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 40
How to fix Datetime in MySQL? 4 49
What is wrong with this PDO Delete Query? 2 17
How do I show metrics with PHP or Javascript 6 32
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question