PDA

View Full Version : Put php-output into a variable


Michiel
06-27-2003, 10:19 PM
Hi,

I was wondering how the following is done:

I've created a template php-page, where I used to include .dat-files with the content. The .dat files are a mix of html-tags and bits of php code (e.g. printing, including and executing a function etc.).

Now I'd like to be able to put the output of a parsed .dat file into a variable, so I can manipulate the content-string (e.g. highlighting search-query's etc.).

How is this done? I've already tried the eval-function, but that doesn't work very well.

I hope you guys understand what I mean and I look forward to your reactions!

Thanx in advance, Michiel

mordred
06-27-2003, 10:38 PM
I understand. :)

Have a look at PHPs output buffering functions. They provide exactly what you are searching for. Especially, have a closer look at these:

http://www.php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-get-contents.php
http://www.php.net/manual/en/function.ob-end-clean.php

Michiel
06-27-2003, 10:52 PM
Hi Mordred,

thanx for your quick reply. I was wondering if this is the right way to do it:


ob_start();

include("test.dat");

$string = ob_get_contents();

ob_end_clean();

$string = highlight($string);

print $string;



Thanx, Michiel

mordred
06-27-2003, 10:58 PM
Basically yes, that looks good. Does it work for you?

Michiel
06-27-2003, 11:04 PM
I don't have acces to my webhost at the moment, so I can't test it right now ... but I guess it should work, right?

I'm gonna try it tomorrow! Thanx again for your help!

Michiel

Michiel
06-28-2003, 03:36 PM
Hi,

I've had a chance to test it out and it works ok. While testing I've come across another problem, with the highlighting-function.

I now use this: $content = eregi_replace($query, "<b>$query</b>", $content);

Because $content also contains html-tags, it is possible that this function messes up the html.

If, for example, the query is 'de' (dutch for 'the') the table-tag with the border-attrinbute gets messed up.

Is there a way to exclude html-tags from the highlighting function??

Thanx again, Michiel

mordred
06-28-2003, 04:39 PM
That's a good question. Unfortunately it's quite complicated to find a sufficient solution. To exclude HTML markup from highlighting, you'd have to write a custom parser that traverses your HTML string and applies the highlighting only when he knows he's not inside any tags. But writing your own parser takes a great amount of time, and it will slow down the highlighting process.

For a news item search I once implemente d I settled for a workaround solution. It was sufficent to me if only whole words, seperated by whitespace and not within any tags was highlighted. Here's a modified version of the method I used to do this. Feel free to extend.


/**
* Highlights text that matches any of the search tokens. Only whole
* words separated by whitespace are highlighted, and not those within
* in HTML tags like attributes etc. The highlighting consists of wrapping
* the match with an em-Tag.
* @param string Text which contains query tokens to highlight.
* @param array All query tokens to be highlighted.
* @return string Highlighted text.
* @access public
*/
function highlight($text, $query) {
$find = array();
$replace = array();

for ($i = 0, $t = $query; $i < count($t); $i++) {
$find[] = '/(\s|\b)(?<!\<)' . preg_quote($t[$i]) . '(\s|\b)(?!\>)/i';
$replace[] = '\\1<em>' . htmlentities($t[$i]) . '</em>\\2';
}
return preg_replace($find, $replace, $text);
}

// test the function
$foo = '<div><table border="1"><tr><td>Test for de div word.</td></tr></table></div>';
$query = array('de', 'div');
print highlight($foo, $query);