View Full Version : Why is there junk in output?

06-04-2007, 06:00 PM
Hi gang...
Once again I am in need of the PHP Gods. Have a look at the code below:

if (is_dir($filename) && (!eregi("_vti_cnf",$filename)) && (!eregi("_overlay",$filename)) && (!eregi("_derived",$filename)) && ($filename!=".") && ($filename!="..")) {
$currdir = array();
$gallery = opendir($filename);
$imgcount = 0;
$gallerytitle = $filename . "/gallery.txt";

//loop to count number of images....

while($f = readdir($gallery)){

if(($f != ".") && ($f != "..")&& (eregi("(\.jpg|\.gif|\.png)$", $f))){

//get a random image to display....

$d = $currdir[(array_rand($currdir))];
$displayimg = sprintf($d,s);
//If the photo folder has a file named 'gallery.txt'
//This will read the first line and stick that text
//into the "alt" of the image (displays on mouseover)

// read title of gallery from gallery.txt

if (file_exists($gallerytitle)) {
$title_file = file($gallerytitle);
$title = $title_file[0];
// if no gallery.txt file was found; show user through $title on webpage
else {
$title = "No description for this gallery was provided";

The first gallery.txt file returns a string no problem, but subsequent strings are preceeded by some junk that looks like "". Each gallery.txt file is a single line of text with no quotes around it or white space. What in the world is causing this, and how do I get rid of it?

06-04-2007, 06:23 PM
Weird characters are usually the result of character encoding issues (UTF-8 versus [insert your favorite format]).

06-04-2007, 06:29 PM
I used ereg_replace to take out the bizzaro stuff; is that the right way to go about this?

06-04-2007, 06:45 PM
First, you should be using preg_replace instead of ereg_replace. It is generally better in all aspects.

Secondly, it would only be a temporary fix in case this is a time critical issue. You would be fixing the symptom, not the cause.

06-04-2007, 07:09 PM
I can change to preg_replace. How/where would I be looking to make an encoding change? And, why would the first one come through ok if the encoding is screwed up? trying to learn....:thumbsup:

06-04-2007, 07:42 PM
Your gallery.txt is encoded in a certain way, and PHP expects a certain encoding.

If the results are not always affected, check the source encoding (the text files).

Otherwise, do some research in how character encoding works in PHP.

It's also possible that it might be a very obvious issue. But without looking at the setup this is what I can tell you.

PHP operates in a certain character encoding. And unless you tell it to read something a certain way, it will see it wrong.

06-04-2007, 08:12 PM
Ok Aedrin,
I'm confused. In gallery.php there is

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
which gives us encoding, right? Where do I find that in a .txt file?

06-04-2007, 09:33 PM
PHP doesn't use that encoding.

That's only for the browser when viewing the page.

I can't tell you what marks a file with a certain encoding.

06-04-2007, 09:36 PM
The PHP Encoding! DOH!!:o
I get it. Let me take a look at THAT!