Jump to content

1 added to included string


Andy17

Recommended Posts

I hope that subject made sense!

 

I have a page where I want to generate page-specific keywords automatically. Actually I have some general keywords stored in a text file and then I add the page-specific ones after those. The problem is, however, solely caused by the keywords I pull from my text file. A "1" is added to my list of keywords. Consider a news page like so:

 

news.php

// ...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="<?php require('php/generateKeywordList.php'); ?>" />
</head>
// ...

 

And then generateKeywordList.php

// I have omitted the part with the page-specific keywords, because it is not what causes the problem (commented it all out)
set_include_path('/mypath/');
$str = require_once('includes/websiteKeywords.txt');
echo $str; // For some reason, the number 1 is added at the end of this string

 

websiteKeywords.txt (it doesn't matter what I put in there)

these, are, my, keywords, for, my, website

 

In my meta tag, the above would be displayed as:

these, are, my, keywords, for, my, website1

 

I then tried to make a simple php page like this

 

$keywords = require('includes/websiteKeywords.txt');
echo $keywords;

 

... and it worked. At the moment I have absolutely no idea where the number 1 comes from.

 

So, basically if I include the keywords directly from the text file into my meta tag, it displays fine. If I make a simple php page where I echo out the keywords from the text file, it displays fine. But if I include my php script, which echos the keywords, into my meta tag, the number 1 is added at the end of the string.

 

Am I completely missing something here or is this extremely strange?

 

 

Thanks for any help!

Link to comment
Share on other sites

$str = require_once('includes/websiteKeywords.txt');
echo $str; // For some reason, the number 1 is added at the end of this string

 

require_once, include_once, include, and require do NOT return the contents of the included file. They output the contents of the included file.  So this first line is OUTPUTing to the browser, the text (keywords) contained in that include file.  Since the require_once() was successful, it returns true which you have assigned to the variable $str.

 

You are putting the 1 at the end of your keywords, when you echo $str since it contains the value true.

Link to comment
Share on other sites

Yeah, I was thinking that it had something to do with that function. I don't know why I didn't look it up. Guess I was too frustrated. Anyways, after reading what you wrote, I made it work by using the file_get_contents function instead.

 

Thank you very much for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.