I have a form that has a textarea in it. How would I make new lines in the textarea appear as new paragraphs when I echo out the submitted textarea value?
<?php
$textarea = $_POST['textarea'];
$newarr = explode("\n", $textarea);
foreach($newarr as $str) {
echo "<p>".$str."</p>";
}
?>
Use nl2br function:
<?php
echo nl2br($_POST['textarea']);
?>
It will print <br> all newlines
echo '<p>' . preg_replace("~[\r\n]+~", '</p><p>', $textarea) . '</p>';
Related
I have the following code:
$data = "Normal text
    code
    code
    code
Normal text";
$data = nl2br($data);
$data= explode('<br />', $data );
foreach($data as $value){
if(preg_match('/^    /',$value)){
echo 'code';
echo '<br />';
}else{
echo 'Not code';
echo '<br />';
}
}
I want to check if each of the lines starts with 4 spaces and if it does i want to echo as 'Code' and if it doesn't i want to echo as 'Not code'. But i am getting the output as 'Not code' though the 2nd , 3rd and 4th lines start with four spaces. I cannot figure out what I have done wrong. Please help me.
nl2br doesn't generate <br /> unless you tell it to. Your explode logic is wrong.
Instead, try this:
$data = "Normal text.......";
foreach(explode("\n",$data) as $line) {
// your existing foreach content code here
}
got it working
added a trim() to get rid of the newline in front of the string
nl2br replace \n with <br />\n (or <br />\r\n),
so when spliting on <br /> the \n is left as the first char
<?php
$data = "Normal text
    code
    code
    code
Normal text";
$data = nl2br($data);
$data= explode('<br />', $data );
foreach($data as $value)
{
if(preg_match('/^    /', trim($value)))
{
echo 'code';
}
else
{
echo 'Not code';
}
echo '<br />';
}
?>
You could also use "startsWith" as defined here...
https://stackoverflow.com/a/834355/2180189
...instead of the regexp match. That is, if the spaces are always at the beginning
I am trying to create a newline character in my foreach loop. After the URL I am trying to create a newline and then have a "------" between the title and content. After this there is supposed to be a space between the next URL. I have the space but I can't seem to get the line break. When I echo "\t" or "\n" in the loop nothing happens. When I use HTML in the loop it won't work, gives too many spaces.
for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
{
echo $URL ['results'][$i]['url'];
echo "---------------";
foreach ($json as $TITLE)
{
echo $TITLE ['results'][$i]['title'];
echo "--------";
}
foreach ($json as $content)
echo $content['results'][$i]['content'];
echo "---------------- ";
}
}
Is there a function in php besides "\n" or another way of manipulating the HTML to insert the line break?
TIA
There are two things you can do:
1: Use <br> as line break. If you use that, you should also consider using <hr> to insert the horizontal line.
some text<br>
<hr>
some other text<br>
2: Use <pre> and </pre> tags around your text to output preformatted text. You can then use \n and \t to format your text.
<pre>
some text\n
-------\n
some other text\n
</pre>
This might the one you are searching for
for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
{
echo $URL ['results'][$i]['url'];
echo "<br/>";
echo "---------------";
foreach ($json as $TITLE)
{
echo $TITLE ['results'][$i]['title'];
echo "--------";
}
foreach ($json as $content)
echo $content['results'][$i]['content'];
echo "---------------- ";
}
}
\n doesn't work in HTML. If you view source of the document you will however see the line breaks. Use html markup like
Test line break<br />
<p>paragraph</p>
<p>paragraph</p>
Simply add <br /> to echo statements. as in html we use this for line breaks.
echo "Content"."<br />";
This would give line breaks in HTML format.
use
echo "<br />";
instead of
echo "---------------- ";
Use echo '';
<?php
echo "first line.<br />Second line.';
?>
or nl2br() function,
<?php
echo nl2br("first line.\nSecond line.");
?>
You can consider
echo "<hr /> ";
<?php
$xml = simplexml_load_file("xmldocumentation.xml")
or die("Error: Cannot create object");
foreach($xml->children() as $pages){
foreach($pages->children() as $page => $data){
echo $data->id;
echo '<br>';
echo $data->timestamp;
//echo $data->revision;
echo "<br />";
echo str_replace("/===[^=]+===/","bold heading here", $data->text);
echo '<p class="text">'.$data->text.'</p>';
}
}
?>
Using php how do i bold the text replaced by php's str_replace function and display the modified content with bold headings ie. ' === Heading === '?
Thanks
Change
echo str_replace("/===[^=]+===/","bold heading here", $data->text);
to
$data->text = preg_replace("/===([^=])+===/","<strong>$1</strong>", $data->text);
You need preg_replace for regex, and need to capture the text you want to bold (via the ()), and then enclose that text in an HTML element that will give you the desired formatting.
A <b> </b> can do the trick!
$strlst = 'lorem ipsum';
$strlst = explode( ' ' , $strlst );
function wrapTag($inVal){
return '<b>'.$inVal.'</b>';
}
$replace = array_map( 'wrapTag' , $strlst );
$Content = str_replace( $strlst , $replace , $Content );
echo $Content;
Upon searching I found PHP function that do inserts before all newlines in a string which is
nl2br();
example:
<?php
echo nl2br("This is an example\r\n where line breaks\r\n added", false);
?>
Above code Output :
This is an example<br\>
where line breaks<br\>
added
What I wanted to have output instead of <br/> I will wrap the string with the tags before and after all newlines
example output from code above wrap string with span
<span>This is an example</span>
<span>where line breaks</span>
<span>added</span>
Is there PHP function exist to this? or a custom PHP function
$str = "This is an example\r\n where line breaks\r\n added";
$str = explode("\r\n",$str);
foreach($str as $key => $value) {
echo "<span>".$value."</span>";
}
You could do an "explode" on "\r\n" and loop over each value with a concatenated span.
Something like
$values = explode("\r\n", "one\r\ntwo\r\nthree\r\nfour")
$newvalues = ""
foreach($values as $value){
$newvalues = $newvalues . "<span>" . $value . "</span>"
}
Use file(). It will return the entire file as an array, each being a new line. Iterate through there and add your span's. Not the best way, but if you have a lot of files to do this for, it's just as easy as any other solution. Otherwise, just explode on your delimiter.
function splitToSpans($string) {
$lines = explode('\r\n', $string);
$finalString = '';
foreach ($lines as $line) {
$finalString .= '<span>' . $line . '</span>';
}
return $finalString;
}
Something like:
$strout = '';
$lines = explode(PHP_EOL, $input);
foreach ($lines as $line) {
$strout .= "<span>$line</span>";
}
Option 1:
<?php
$string = "Line 1
Line 2
Line 3";
$string = preg_replace('/^(.*)$/m', '<span>$1</span>', $string);
echo $string;
Option 2:
<?php
$string = "Line 1\r\nLine 2\r\nLine 3";
$string = array_map(function($value) {
return "<span>$value</span>";
}, explode("\r\n", $string));
echo implode("\r\n", $string);
This function splits the string based on either a CRLF or LF only and then wraps it into a <span> tag, applying proper escaping (important):
function nl2span($str)
{
$r = '';
foreach (preg_split("/\r?\n/", $str) as $line) {
$r .= '<span>' . htmlspecialchars($line). '</span>';
}
return $r;
}
If your line endings are always CRLF you can replace the preg_split() with a more conventional explode("\r\n", ...).
$string = "This is an example\r\n where line breaks\r\n added";
var_dump($string);
// string(46) "This is an example
// where line breaks
// added"
array_map(function($s){echo sprintf('<span>%s</span>', trim($s));}, explode("\r\n", $string));
// <span>This is an example</span>
// <span>where line breaks</span>
// <span>added</span>
This is useful:
function nl2span( $str) {
return '<span>' . implode( '</span><span>', explode( "\r\n", $str ) ) . '</span>';
}
Read through the answers, and noone had mentioned this solution...
$string = "This is an example\r\n where line breaks\r\n added";
echo '<span>' . str_replace('\r\n', '</span><span>', $string) . '</span>';
here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)