Results 1 to 3 of 3

Thread: [RESOLVED] str_replace(), ob_start() and UTF-8

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    [RESOLVED] str_replace(), ob_start() and UTF-8

    Hello all.
    Trying to build an application which replaces strings within an html page with elements from an array.
    I can't figure out what I'm doing wrong here. I've got an external file named ru.txt. It has Russian text in it, hopefully it will display here:
    Code:
    <?
    
    	$txt[0]="что компания является";
    
    ?>
    My index page, which includes the text file, looks like this:
    Code:
    <?php
    
    	/* MAKE CERTAIN WE USE UTF-8 ENCODING */
    		header("Content-type: text/html; charset=utf-8");
    
    	/* INCLUDE THE APPROPRIATE LANGUAGE FILE */
    		include_once('_includes/ru.txt');
    
    	/* THE CALLBACK FUNCTION */
    		function callback($buffer) {
    
    			/* LOOP THROUGH THE ARRAY, MAKING THE APPROPRIATE REPLACEMENTS */
    				for($i=0; $i<count($GLOBALS['txt']); $i++) {
    
    					$buffer=str_replace('{txt_'.$i.'}',$GLOBALS['txt'][i],$buffer);
    
    				}
    
    			/* RETURN IT */
    				return $buffer;
    		}
    
    	/* BEGIN THE OUTPUT OBJECT */
    		ob_start("callback");
    
    ?><html>
    	<head>
    		<title>test</title>
    		<meta http-equiv="Content-type" value="text/html; charset=utf-8">
    	</head>
    	<body>{txt_0}</body>
    </html><?
    
    	/* END THE OUTPUT OBJECT */
    		ob_end_flush();
    
    ?>
    I am getting an empty page (nothing within the body tags.)
    Both files have been saved with UTF-8. If I hard-code the str_replace with something like "blah" it works fine. If I remove the whole callback procedure and echo the $txt[0] from the external file, it works fine.
    Any clues as to what I'm doing wrong?
    Muchos thanks.
    Last edited by solitario; Sep 24th, 2008 at 09:05 AM. Reason: resolved

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: str_replace(), ob_start() and UTF-8

    Duh, I fixed it. Doing too much JavaScript lately. I forgot the $ in the second parameter of the str_replace.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] str_replace(), ob_start() and UTF-8

    You should set the error reporting level so you can see errors like that. It will make the task of debugging 1000 times easier. In the PAP.ini file change the line error_reporting to:
    Code:
    error_reporting = E_ALL
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width