PHP: including files and converting special characters?

  • davenewt
  • Graduate
  • Graduate
  • User avatar
  • Posts: 160

Post 3+ Months Ago

8) Hi folks,

I'm pulling together a site and I'd like to include text files to populate the content of HTML pages.

I'm using PHP 4.2.2 and, as I'm new to PHP, I'll need a little help if you don't mind.

The problem is that a standard
Code: [ Select ]
include("file.inc");
(file.inc being a simple unicode text file) doesn't work because this file contains both HTML tags (span, table etc) and special characters in the text (ampersands, apostrophes, hyphens etc).

Using the include function, the HTML tags are interpreted correctly (tables appear as tables, etc!) but special characters in the 'body text' are not.

I have had a quick look on php.net and thought the file_get_contents function would be the thing to use, but trying to create a string using this function...
Code: [ Select ]
$thispagecontent=file_get_contents("includes/text.inc");
...returns...
Code: [ Select ]
Fatal error: Call to undefined function: file_get_contents()


Even though this function is in PHP 4.2.2... ? The intention was then to use
Code: [ Select ]
echo htmlentities($thispagecontent);
to convert all the special characters - but thinking about it, wouldn't this convert all the "<" and ">" characters used for HTML tags, and result in my HTML tags not being recognised?

I'm obviously going about this completely the wrong way. Can someone tell me which functions I should be using to get the result I need?

Thanks,
Dave.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • panreach
  • Novice
  • Novice
  • User avatar
  • Posts: 28

Post 3+ Months Ago

no no, you were write on track with the first one. use include. however don't use .inc because you're code is not secure. when an .inc file is opened in the browser by itself it is treated as a text file. (php isn't)

yes html and php can obviously exist on the same page.

Code: [ Select ]
<!-- some.html -->
<html>
<body>
<?
include("somecontent.php");
?>
</body>
</html>
  1. <!-- some.html -->
  2. <html>
  3. <body>
  4. <?
  5. include("somecontent.php");
  6. ?>
  7. </body>
  8. </html>



or


Code: [ Select ]
<!-- some.html -->
<html>
<body>
<?
$someincludepath = "somepage.php";
include("template.php");
?>
</body>
</html>


<!-- template.php-->

<span id="cooltemplate">
<?=$someincludepath?>
</span>




<!--sompage.php-->
<?
echo "groovy";
?>
  1. <!-- some.html -->
  2. <html>
  3. <body>
  4. <?
  5. $someincludepath = "somepage.php";
  6. include("template.php");
  7. ?>
  8. </body>
  9. </html>
  10. <!-- template.php-->
  11. <span id="cooltemplate">
  12. <?=$someincludepath?>
  13. </span>
  14. <!--sompage.php-->
  15. <?
  16. echo "groovy";
  17. ?>


I think as you'll see with the second post, you can do some serious stuff. I personally use this system:


index.php file calls

application.php which includes classes and library files. also starts my session and creates a connection to mysql

then i pass php ?somevar=whatever

when somevar = blah

$includepath = "blah.php";
include("template.php");

the template page is the basic html body and style of the site. blah.php is my content. Doing it this way allows template.php to be the only page you design for your site. all the other pages are just content pages. so when somevar==apple

$includepath = "myappletree.php";


template.php would look like

<html>
<body>

<?=$includepath?>

</body>
</html>

-------------
oh important. Make the variable in your php.ini system file have the variable

register_globals = on

this way you can pass variables to other pages without having to request them.

-panreach
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Just a tip.

Register_globals set to on is said to creat security holes and is recommended that you build PHP apps without this enabled.

If you want to use .inc as an extension and have your code secure as panreach describes then just set your server to process .inc files the same as .php files - this effectively closes the hole.
  • Sabu
  • Beginner
  • Beginner
  • Sabu
  • Posts: 47

Post 3+ Months Ago

Code: [ Select ]
$lines = file('textfile.inc');


Will open the entire shebang into an array $lines, which you can then go through and manually replace all the characters you don't like.

This is still presuming that file_get_contents isn't working for you.

From my extensive research it looks like functions like htmlspecialchars are generally used for the display of html, but not for the inclusion of it.

As for certain characters not appearing for you, well.. I can't really help, I'm afraid.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

For replacing our stripping out HTML characters you would have to look at using a regular expression (wtf is a regular expression)

Anyways check out http://www.regular-expressions.info/ for more info

probably use something to the effect of

PHP Code: [ Select ]
 
$output = ereg_replace("<[^>]>","",$inputStr );
 
 
  1.  
  2. $output = ereg_replace("<[^>]>","",$inputStr );
  3.  
  4.  


for your special characters just loop throught the contents of the file and replace any occurence of these characters with the actual characters and then vice versa for saving.

the code listed below will open the file, replace all the special characters with the set values and then you can output the value to a text box
PHP Code: [ Select ]
 
$fileN = "text.txt";
 
$handle = fopen ($fileN, "r");
 
$fileContents = fread ($handle, filesize ($fileN));
 
fclose ($handle);
 
 
 
$output = $fileContents;
 
$special_chars = array("&nbsp;","&amp;","&copy;");
 
$replace           = array(" ","&","c");
 
for ($i=0;$i<count($special_chars);$i++){
 
 $output = ereg_replace($special_chars[$i],$replace[$i],$output);
 
}
 
echo "<textarea name='salami'>$output</textarea>";
 
 
  1.  
  2. $fileN = "text.txt";
  3.  
  4. $handle = fopen ($fileN, "r");
  5.  
  6. $fileContents = fread ($handle, filesize ($fileN));
  7.  
  8. fclose ($handle);
  9.  
  10.  
  11.  
  12. $output = $fileContents;
  13.  
  14. $special_chars = array("&nbsp;","&amp;","&copy;");
  15.  
  16. $replace           = array(" ","&","c");
  17.  
  18. for ($i=0;$i<count($special_chars);$i++){
  19.  
  20.  $output = ereg_replace($special_chars[$i],$replace[$i],$output);
  21.  
  22. }
  23.  
  24. echo "<textarea name='salami'>$output</textarea>";
  25.  
  26.  


BEWARE this code does not check (might also be buggy, just hacked it together now) for the existence of the file, so if it works for you just remember to do file exists checks and any other checks you might have
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

You don't need to do a loop for the ereg_replace, it should take arrays.

The only problem with this is that you will have to watch the & character. Make sure it si the first element in the array. Also make sure that you don't have any special characters in there to begin with, else "&nbsp;" will become "&amp;nbsp;".

There is a better way that uses a callback to replace the $searchFor character with the corresponding ascii code, meaning that you can use an OR in the replace, which would make it a hell of a lot more efficient. It would also prevent you having to manually match up the characters and their replacements.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

So glad you could join us!

So if there is a better way show us! Been doing it like this for yonks and it is pretty frustrating sometimes.

Would love to know a better way :lol:
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Rabid Dog wrote:
So glad you could join us!

So if there is a better way show us! Been doing it like this for yonks and it is pretty frustrating sometimes.

Would love to know a better way :lol:


http://uk.php.net/manual/en/function.pr ... llback.php

TBH , I dno't actually get how that function works, I'm not sure what the [0] [1] and [2] are for.

Basically the idea is that you have a string of bad character:
Code: [ Select ]
&badChars="/(&|£|$|~)/"
that you want to replace

They are delimited and in brackets with a pipe between them (as a logical OR)

So when you use the preg_replace_callback() it will pass the characters it finds to a callback function. The callback would take the single character string and replace it with "&#", {the ascii code}, and then ";"

Now I looked more closely at the preg_replace_callback() function and I don't really get it so I can't build an example :roll:

This is better because:
a) It doesn't require you to maintain 2 arrays and ensure that the replacements are correct
b) It will only do one pass through the string, as opposed to one for each character (this could save you several milliseconds, possibly even centiseconds :shock: )

But yeah, I'm not sure how to get it to work so I'm gonna shut up now :roll:
  • davenewt
  • Graduate
  • Graduate
  • User avatar
  • Posts: 160

Post 3+ Months Ago

Thanks guys. I'm still having hassle though.

On panreach's suggestion, I have renamed the file as .php rather than .inc, but using
Code: [ Select ]
include("includes/sometext.php");
still results in special characters not being displayed correctly.

What I need to do in the long run is learn how to use PHP and MySQL to create a database of page content and do all this properly, I know. :(

But until that point (!) I still need some way of including a separate file whilst converting the special characters OUTSIDE of HTML tags which apply styles to the text.

Can PHP not do this natively?

Cheers,
Dave.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: Bigwebmaster and 104 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.