How can I print a php file (source) in the browser without the php tags at the start and at the end?
So if my php file looks like this:
<?php
if( true ){
echo 'hello world';
}
?>
And in another file I want to load that file and echo the if statement so from the line 2 to 4, but not loosing the tabbing.
I have tried: fgets() and file_get_content() functions, butI can only echo an non-tabbed data.
Is there a method for this problem? or I have to write code for tabbing the source?
This should work for you:
(Here I get all lines into an array with file(). Then I cut out the first and the last line with array_slice(). After this I simply loop through the array an print it)
<?php
$lines = file("file.php");
$lines = array_slice($lines, 1, count($lines)-2);
foreach($lines as $line)
echo str_replace(" ", " ", $line) . "<br>";
?>
output:
if( true ){
echo 'hello world';
}
Related
<?php
$color='red';
$lines=file("new.txt");
foreach ($lines as $line) {
if($line=='<~~~>'){
if($color=='red')
echo $color='yellow';
elseif($color=='yellow')
echo $color='red';
}
else{
echo $line."<br>";
}
}
?>
File Content:
hey there how are you
<~~~>
I am fine What about you?
<~~~>
I am also good. thank you.
<~~~>
Output is coming:
hey there how are you
<~~~>
I am fine What about you?
<~~~>
I am also good. thank you.
yellow
There's a trailing newline char at the end of each line. Use trim($line) to get rid of it :
<?php
$color = 'red';
$lines = file("new.txt");
foreach ($lines as $line) {
if (trim($line) == '<~~~>') {
if ($color == 'red')
echo $color = 'yellow';
else if ($color == 'yellow')
echo $color = 'red';
echo "<br />"; // Added to keep the original format
} else {
echo $line . "<br />";
}
}
Output :
hey there how are you
yellow
I am fine What about you?
red
I am also good. thank you.
yellow
It is explained in the documentation of PHP file() function:
Return Values
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached.
This means, except for the last line, in case it doesn't end with a new line character, no line in your file can be equal to string '<~~~>'.
The solution(s) are also provided in the same documentation page, one paragraph below:
Note:
Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.
Your problem is
echo $color='yellow';
just do
echo 'yellow'
instead. Same goes for the echo 'red'
The expression "$color = 'yellow'" produces a return value of 'void' which then is seen by the "echo". So actually you're doing a "echo void;" equivalent.
I have a string of code like this:
<?php
echo "Hello World";
?>
Now, I want to write that into a php file just the way it is. What I mean is that I want to write this code with new line characters. So the code gets stored in the above fashion. So, how can I do that?
I had tried to use file append and fwrite, but they write the code in one consecutive line and what I want is to have it divided into 3 lines.
On the first line - <?php
On the second line - echo "Hello world";
On the third line - ?>
But I would like to do it by using only one line of code, that would be something like the below one.
$file = 'people.php';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
This should work fine using an heredoc:
file_put_contents('people.php', <<<HEREDOC
<?php
echo "Hello World";
?>
HEREDOC
);
<?php
$File = "new.txt"; //your text file
$handle = fopen($File, 'w');
fwrite($handle, '<?php '."\n".' echo "Hello World "; '."\n".'?>');
?>
Your string should be like this
$str = "<?php
echo \"Hello World\";
?>";
then write it to any fileName.php, php while parsing ignores new lines, only semicolons matters.
Edit 1
Since you want to write your string as code, after following above step you will have all your code in a single line more like a compressed code, which will be not human readable, for making it human friendly, you will have to add formatting, a minimal approach for formatting will be to at least add new line chars and tabs for indentation.
for this you will have to do two things, identify chars (a) where you need to add new line feed and those chars (b) where indentation is required. Now before writing to file do some preprocessing add new line chars for char type (a) and at the same time maintain a stack for adding proper number of tabs for indentation.
Try using the following between each line of code to put it on a new line in the php file:
. PHP_EOL .
You can:
<?php
$yourContent = <<<PHP
<?php
echo "Hello world";
echo "This in an example";
$foo = 2;
$bar = 4;
echo "Foo + Bar = ".($foo+$bar);
?>
PHP;
file_put_contents("yourFile.php", $yourContent);
?>
Try following :
<?php
$file = "helloworld.php";
$content = file_get_contents($file);
print_r(htmlspecialchars($content));
?>
Helloworld.php :
<?php
echo "Hello World";
?>
It'll work fine.
It sounds like you need to add a line break to the end of each line. You can use a regular expression search/replace as follows (where $newData already contains the string you want to append to the file):
<?php
$newData = preg_replace('/$/',"\n", $newData);
file_put_contents($file, $newData, FILE_APPEND | LOCK_EX);
?>
This finds the end of each line (indicated by $ in regex) and adds the new line (\n) at that position.
this code is working on my pc. you will also like it.
<?php
$file = 'people.php';
// The new person to add to the file
$person = "<?php
echo 'Hello World';
?>\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
echo $person;
?>
I'm reading content from a text file which I'm showing on a page later, this is my code:
$lines = file("content.txt");
$i=1;
foreach($lines as $line ){
$var["line" . $i] = $line;
$i++;
}
extract($var);
The text file includes content in this format:
bla1
bla2
and so on, there's no space behind the domains just a linebreak, now I want to concatenate the content and show it, so I do this:
$as1 = $line1.$line2;
echo $as1;
But instead of the expected result
Bla1Bla2
I get
Bla1 Bla2
What am I doing wrong ? I can assure that there's no space in the text file not behind nor infront of the content.
There's no space; but unless you tell the file() function otherwise, there is a line feed at the end of each line
$lines = file("content.txt", FILE_IGNORE_NEW_LINES);
A browser will render a line feed as a space, unless inside a or block
use trim for this situation
$as1 = trim($line1).trim($line2);
echo $as1;
You could try trimming your input...
$lines = file("content.txt");
$i=1;
foreach($lines as $line ){
$var["line" . $i] = trim($line);
$i++;
}
extract($var);
I'm using PHP and I want to upload a text file, with the output / view showing each line from the text file on a new line (so exactly as it is displayed in the text file).
However, I also want the text file to be sorted alphabetically.
I have working code that uploads the file on each new line, and sorts by upper case, then lowercase - using the sort function
And I have code which sorts it alphabetically (regardless of case), but unfortunately the lines are grouped together, and not separated as I want them to be. - using the natcasesort
I've tried numerous things but not getting anywhere, so hoping someone can help either put the two together, or let me know what I need to do to either piece of code which will make each line show on a new line.
1st CODE NEEDS TO BE SORTED ALPHABETICALLY REGARDLESS OF UPPER/LOWERCASE
2nd CODE NEEDS TO SHOW THE LINE BREAKS
<?php
$file = file("users.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
}
?>
<?php
$filename="users.txt";
$lines = array();
$file = fopen($filename, "r");
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
print_r($lines);
fclose ($file);
?>
You must use
$text = implode("<br />", $lines);
echo $text
It would glue the array and pasting a <br /> in every new line. Of course, if it is being printed in HTML, if not use the carrier \n instead.
And your code would look like:
.
.
.
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
$text = implode("<br />", $lines);
print_r($text)
.
.
.
I've learned it's possible to trim a string from a textarea and put break tags after it, so each sentence written at a new line in the textbox will also be written at a new line in the PHP file.
This is the snippet:
<html>
<body>
<?php
$text = trim($_POST['textarea']);
$text = nl2br($text);
echo $text;
?>
</body>
</html>
The thing is that my true intentions are:
Use the contents of each line in the textbox for a certain script
Print the contents of each line with the results from the script added all separated by lines.
<?php
$text = trim($_POST['textarea']);
$text = explode ("\n", $text);
foreach ($text as $line) {
echo myFunction($line);
echo "< hr />";
}
?>
The PHP function explode lets you take a string and blow it up into smaller pieces. Store this array for use in other scripts
$str_arr = explode("\n", $_POST['textarea']);
//$str_arr can be used for other script