I would like to erase   in Wordpress post when we skip a line but keeping the "skip a line".
In post.php I've added this function :
function remove_empty_lines( $content ){
$content = preg_replace("/ /", "\n", $content);
return $content;
}
add_action('content_save_pre', 'remove_empty_lines');
but the \n doesn't work, what can I write for that works ? (<br /> doesn't work too).
\n doesn't represent a new line in HTML, so you won't see the line break when you echo the result. Either use "<br />" directly as the replacement, or use the default nl2br() PHP function to insert HTML line breaks before PHP line breaks e.g.
$sample = "testing a \n line break";
echo $sample;
// HTML output is:
"testing a line break"
$sample2 = "testing a <br /> line break";
echo $sample2;
// HTML output is:
"testing a
line break"
$sample3 = "testing a \n line break";
$sample3 = nl2br($sample3);
echo $sample3;
// HTML output is:
"testing a
line break"
Newlines in HTML are expressed through <br > or <br/>, not through \n.
$content = str_replace(" ", '<br/>', $content);
echo nl2br($content);
Related
I have PHP set up for a textarea that prints text from a directory to separate divs.
<?php
$content = implode("<br /><hr class='separator'>",array_map(function ($v) {
return file_get_contents($v);
}, glob(__DIR__ . "/posts/*.txt")));
echo '<div>';
// echo htmlentities($content);
echo '</div>';
?>
When I use htmlentities (commented out above), it disables my line break (and also my hr separator code).
I tried using \n but it also doesn't work.
How do I keep my <br /><hr class='separator'> code but still use htmlentities?
Call htmlentities() before inserting the HTML separators.
$content = implode("<br /><hr class='separator'>",array_map(function ($v) {
return htmlentities(file_get_contents($v));
}, glob(__DIR__ . "/posts/*.txt")));
echo "<div>$content</div>";
I've one variable, say $text as follows :
$text = "Line one <br>Line two<br>Line threee<br>Line Fourrr<br>Line Fiveee";
I've written one function called function br2nl() as follows :
function br2nl($buff = '') {
$buff = mb_convert_encoding($buff, 'HTML-ENTITIES', "UTF-8");
$buff = preg_replace('#<br[/\s]*>#si', "\n", $buff);
$buff = trim($buff);
return $buff;
}
Following is the call to the function :
$text = br2nl($text);
Now if I echo $text; I'm getting blank. The expected result should be all the <br> tags get replaced by <\n>. The expected output should be as follows :
"Line one\nLine two\nLine threee\nLine Fourrr\nLine Fiveee"
So what should I do?
I'm using fopen() but I get this error on execution.
Parse error: syntax error, unexpected '/' in /home/furtherpath.. on line 7
The line 7 is:
/home/a11*****/public_html/rishi/rishi_someone_php.php = fopen("rishi_someone_php.php","r");
I know I should place a file handler instead but that also doesn't seem to work and gives the same error.
Can't figure out why.
What I'm doing is creating a html page using php.:
$html="<html> \n <head> \n <title>".$fn."</title> \n </head> \n <body> \n <?php \n $file=fopen('".$full."','r'); \n while(!(feof($file))) \n { \n echo htmlspecialchars(fgets($file)).'<br/>'; \n } \n fclose($file); \n ?> \n </body> \n </html>";
Thanks in advance.
Here is a cleaned up version of your script. Try to understand it. I'e combined the string at each html element just to be clear. There is no reason for php tags within php tags. In your code at this line $file=fopen('".$full."','r');
You are trying to quote a variable of type string. That is not necessary with variables. PHP will know that it is a string, so don't do that. Only use quotes when a string literal is being passed into the function as an arguments.
$html = "<html>\n<head><title>". $fn."</title>\n</head><body>\n";
$fp = fopen($full, "r");
if ($fp) {
while (($line = fgets($fp)) !== false) {
$html .= htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
$html .= "<br />";
}
} else {
$html .= "No data";
$html .= "<br />";
}
fclose($fp);
$html .= "</body>\n</html>\n";
echo $html;
Anything wrong with this code? I want it to print the name and address - each on a separate line, but it all comes up in one line.
Here's the code
<?php
$myname = $_POST['myname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$town = $_POST['town'];
$county = $_POST['county'];
$content = '';
$content .="My name = " .$myname ."\r\n";
$content .="Address1 = " .$address1 ."\n";
$content .="Address2 = " .$address2 ."\n";
$content .="Address3 = " .$address3 ."\n";
$content .="town = " .$town ."\n";
$content .="county = " .$county ."\n";
echo $content;
?>
It looks like the '\n' character is not working.
In your source code this will show on a next line, but if you want to go to another line in HTML you will have to append <br />.
So:
$content .="My name = " .$myname ."<br />\r\n";
I left the \r\n here because it will go to the next line in your source code aswell, which might look nicer if you have to view the source.
The \n character properly works just fine. The problem is, it's not what you expect.
If you see this in a browser, you won't see line breaks, because line breaks are ignored in the source code. The HTML parser only reads <br> as line breaks.
If you try to go to your website and view the source code, you'll find that the line breaks are in there.
<?php
$start_date = "20111101";
$todays_date = date('Ymd');
$html = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$start_date}.html?mod=mdc_pastcalendar");
preg_match_all(
'#<td style="text-align:left;padding-top:18px;" valign="bottom" class="colhead">(.*?)</td>#',
$html,
$EXCHANGE,
PREG_PATTERN_ORDER);
preg_match_all(
'#<td class="num">(\d.\d\d)</td>#',
$html,
$TRIN,
PREG_PATTERN_ORDER);
echo "{$EXCHANGE[0][0]}, \r";
echo "TRIN : {$TRIN[0][0]}, \r";
echo "{$EXCHANGE[0][1]}, \r";
echo "TRIN : {$TRIN[0][1]}, \r";
echo "{$EXCHANGE[0][2]}, \r";
echo "TRIN : {$TRIN[0][2]}, \r";
echo "{$EXCHANGE[0][3]}, \r";
echo "TRIN : {$TRIN[0][3]}, \r";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
?>
Why is the output in WSJData.csv different from the echo output? Why do I get the text I removed with the preg_match_all function in my CSV file?
If you want to write the result of some echo calls to a file, use:
ob_start(create_function('$a','file_put_contents(FILENAME,$a); return $a;'));
echo ...
echo ...
...
echo ...
ob_end_flush();
This will ensure that what is printed is what goes in the file.
Sorry, it seems to me that you have some other error in your script. I reduced it to "the simplest program that works" to reproduce the error. Here is what I got:
<?php
$TRIN = array( array("E0","E1","E2","E3") );
echo "TRIN : {$TRIN[0][0]} / ";
echo "TRIN : {$TRIN[0][1]} / ";
echo "TRIN : {$TRIN[0][2]} / ";
echo "TRIN : {$TRIN[0][3]} \n";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
echo file_get_contents($WSJData);
And the output is:
$ php test.php
TRIN : E0 / TRIN : E1 / TRIN : E2 / TRIN : E3
E0, E1, E2, E3
The first line of output is from the echoes. The second is from "file_get_contents".
The substitution works fine. Indeed, it occurs when assigning the variables, not when echoing or saving to the file.
BTW, I would recommend using \n instead of \r. The second one will not advance a line, but overwrite the current one (just in the console as it would in the printer), and I suspect that that's not what you wanted.