File_get_contents doesn't import php? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this code:
elseif($pagina == "agenda")
{
$html = file_get_contents("template/index.php");
$html = str_replace("%content%",file_get_contents("template/agenda.php"),$html);
$html = str_replace("%current2%",'class="current"',$html);
$html = str_replace("%sidebar%",file_get_contents("template/sidebar/sidebar_nieuws.php"),$html);
}
On the page "agenda.php" is a normal php script. This does work when I open agenda.php directly, but when I open it with this code, the code doesn't work. I think file_get_contents doesn't work on php? What is an alternative?
(Sorry for my bad english ;)..)

I would use output buffering. It's effective in this kind of scenario.
$html = file_get_contents("template/index.php");
$arr = array();
ob_start();
include_once('template/agenda.php');
$file = ob_get_contents(); //$file now stores the rendered output (after PHP has run) of template/agenda.php
ob_end_clean();
$arr['agenda'] = $file;
$html = str_replace("%content%", $arr['agenda'], $html);

if you want to run the php code. Then you can include your php file:
include('path/to/file.php');

if you want to change php code from file before executing then in the end run eval($html);

Related

why i have a break line in the beginning of my csv with php? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i keep getting a break line at the beginning of the csv i've create, and in my array there isnt a break line.
here is my code:
$file = 'prueba.csv';
$filew1 = fopen($file, 'w');
foreach ($csv as $line) {
fwrite($filew1, $line . PHP_EOL);
}
fclose($filew1);
Yii::$app->response->SendFile($file);
and my out put is like this
i dont know why, i have try the str_replace() metod, still.
i have fix it, putting a ob_clean(); at the beginning of the method.

Shorten String function not working properly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this little function to stop a string being too long but it dosen't seem to work. I'm assuming i've done something wrong?
function trimString($string, $maxChar) {
$string = (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}
I was using it like this:
echo trimString($row['mainTitle'], 30);
Thanks
You forgot to return value from function. Try
function trimString($string, $maxChar) {
return (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}

file_get_contents doesn't work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
$z = file_get_contents("vars.txt");
var_dump($z);
This is my current code. When I run it I see this output:
string(0) ""
I am not sure why - this file contains 321 characters. It is in the same directory with my PHP script. Can you explain me reasons, why that doesn't work? Writing to this file works ok (with "w+" mode).
I though this is chmod mistake, but I wrote chmod 777 dir command, and still doesn't work.
Full code of my script is there: https://gist.github.com/ty221/274bda4ecec03f710691
Displaying all errors E_ALL doesn't work - I mean nothing is getting displayed.
How about reading the manual: http://php.net/manual/en/function.fopen.php
And a quote from there:
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
^^^^^^^^^^^
That means with your line:
//Deletes all content in the file
$file = fopen("vars.txt", "w+");
$to_save = "";
//Reads the content from a empty file
$z = file_get_contents("vars.txt");
//Outputs an empty string
var_dump($z);

simplexml_load_file() with a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm currently having an issue with simplexml_load_file(); my xml path is a url, that is rendered as a variable
$xurl = "domain/pathto/myfile.xml"; // This is actually a variable that returns the entire URL to where my xml file is -- this will change from file to file
$xmlpath = parse_url($xurl, PHP_URL_PATH); // to get the path of my xml file ex. /pathto/myfile.xml
$xmlpath = mb_substr($xmlpath, 1); // returns pathto/myfile.xml
here is where my problem is, when I put it into :
simplexml_load_file($xmlpath);
In my function, I get nothing appearing from the XML file
However, in my same function if I change it to
simplexml_load_file("pathto/myfile.xml");
My function works fine.
I did an echo on $xmlpath and it returns the pathto/myfile.xml just fine.
<?php echo $xmlpath; ?> // returns pathto/myfile.xml
What am I doing wrong?
EDIT: Phil
echo strcmp("pathto/myfile.xml", $xmlpath)
returns a 0.

What is wrong with strpos()? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Have code:
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos = strpos('/page', $url)) echo '1';
else echo '2';
It shows only '2'.
How can I fix it?
Change arguments places
strpos($url, '/page')
strpos
Actually the syntax for function was mismatched I think.
use this
$stropos = stripos($url,"/page");
echo $strpos;
Please add === identical compare in if condition and correct the position of strpos parameters
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos == strpos($url, '/page')) echo '1';
else echo '2';

Categories