Is there a way in PHP array to show line break? [duplicate] - php

This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 4 years ago.
I have an index.php file and an array which has message. Is there a way that instead of <br> tag I can display the text with a new line in PHP so I can also store it in database?
The code:
$array = array(
array(
'id' => 1,
'message' => 'I\'m reading Harry Potter!',
),
array(
'id' => 2,
'message' => 'Ok. I just got a notification that you sent me a pin on Pinterest.<br>Will you come to school tomorrow?',
)
);
For example:
Ok. I just got a notification that you sent me a pin on Pinterest.
Will you come to school tomorrow?

The new line character is \n. Simply replace <br> with \n and you will have the results you're looking for.
PHP - how to create a newline character?
Note!
php does not process escape characters within single quotes.
'\n' is not processed as a new line character, while "\n" is.
What is the difference between single-quoted and double-quoted strings in PHP?
Depending on your platform, you may want to be more specific about which new line sequence you choose.
\r\n, \r and \n what is the difference between them?
$array = array(
array(
'id' => 1,
'message' => "I'm reading Harry Potter!"
),
array(
'id' => 2,
'message' => "Ok. I just got a notification that you sent me a pin on Pinterest.\nWill you come to school tomorrow?"
)
);
echo "<pre>";
print_r($array);
exit;
This will show you the raw formatting of your string.
To then convert new lines to the <br> tag for display on a webpage, you would pass that string to nl2br()
<?php echo nl2br($array[1]['message']); ?>

Is there a way that instead of <br> tag I can display the text with a new line in PHP
Yes you can easily do this with CSS
white-space: pre;
https://www.w3schools.com/cssref/pr_text_white-space.asp
Back in the day I used to do the whole "replace" thing, then I got bored of it. Now I just use CSS.
The pre option/setting will preserve whitespace much like using the <pre> tag. The only thing you have to watch for is indenting in the source code
<p style="white-space:pre;">
<?php echo $something; ?>
</p>
This extra space in the code will be added to the PHP output, instead do this:
<p style="white-space:pre;"><?php echo $something; ?></p>

You can close the php tag and reopen after the break.
For example -
'message'=>'Ok.I just got a notification that you sent me a pin on Pinterest. ?>
<br>
<?php Will you come to school tomorrow?',

Related

How to add a line break in a string, this is some Drupal function

I want a line break in the #attributes, I've mentioned it in the code as in where I want the line break to be.
$form['advanced_user_data'] = array(
'#type' => 'textarea',
'#attributes' => array('placeholder' => array('"Jack Polard" <jackpolard#xyz.com> (Want a line break here) "Miky Town" <miky#abc.com> (Want a line break here) "Bill Gates" <bill#example.com>')),
'#description' => t('Eg: "Full Name" < emailid#domain.com >'),
);
See this is how it has to be, "\r\n" didnt work, I'm a HTML guy, so I have no knowledge of PHP, please help me in a simpler way.
i think your linebreaks should used in a textarea use
this is command for newlines in html. give this a try.
// edit after comment
try
'#attributes' => array('placeholder' => array('"Jack Polard" <jackpolard#xyz.com>
"Miky Town" <miky#abc.com>
"Bill Gates" <bill#example.com>')),
if this and the other solutions ... \n etc... not work, i think the output to your textarea gets htmlescaped, so you can't use linebreaks as long the complete output gets escaped.
Most of the time \n or \r\n or PHP_EOL will do the trick. Just try which one works for you. If you echo to html you should use <br>.

How to add a line break into a mailto link that is being encoded with PHP's rawurlencode

I have something like this.
<a href='mailto:mike#example.com?subject=<?php print rawurlencode('This is the subject.');?>&body=<?php print rawurlencode('Line 1 of body. Line 2 of body.');?>'>Email</a>
I want to put a line break in the body of the message. I found via google that you can put %0D%0A into the mailto to do a line break but the problem is that rawurlencode will just encode it. What could I put into rawurlencode in order to get the line break to show up in the message.
You can add \r\n characters in your string if you use double quotes, this will avoid the double encoding issue you're describing.
<?php
echo rawurlencode("Line One.\r\nLine Two.");
/* Line%20One.%0D%0ALine%20Two. */
The http_build_query function might be handy too.
<?php
$params = array(
'subject' => 'Subject',
'body' => "Line One.\r\nLine Two."
);
echo http_build_query($params);
/* subject=Subject&body=Line+One.%0D%0ALine+Two. */
\n - this is called linefeed - special character

removing unneeded query from generated url string in php

I have the following string generating mp3 urls for a music player on my site.
<?php echo $song->getTitle() ?>
which results in /public/music_song/df/74/746b_2112.mp3?c=ec1e
I would like to remove the query from the string
resulting in /public/music_song/df/74/746b_2112.mp3
I've looked into how to split the url, but I'm nowhere near being a php genius just yet so I dont know weather to split or use preg_replace or how the heck to incorporate it into my existing string.
I have to get rid of these queries, they are unneeded and crashing my databases on a daily basis.
list($keep) = explode('?', '/public/music_song/df/74/746b_2112.mp3?c=ec1e');
$parsedInput = parse_url('/public/music_song/df/74/746b_2112.mp3?c=ec1e');
echo $parsedInput['path'];
// Results in /public/music_song/df/74/746b_2112.mp3
Edit: Since I havent worked with SocialEngine, Im guessing that what you need to do is:
<?php $parsed = parse_url($song->getFilePath());
echo $this->htmlLink($parsed['path'],
$this->string()->truncate($song->getTitle(), 50),
array('class' => 'music_player_tracks_url',
'type' => 'audio',
'rel' => $song->song_id )); ?>

Search dynamic term twice in Regex

I know I can refer in replacement to dynamic parts of the term in regex in PHP:
preg_replace('/(test1)(test2)(test3)/',"$3$2$1",$string);
(Somehow like this, I don't know if this is correct, but its not what I am looking for)
I want that in the regex, like:
preg_match_all("~<(.*)>.*</$1>~",$string,$matches);
The first part between the "<" and ">" is dynamic (so every tag existing in html and even own xml tags can be found) and i want to refer on that again in the same regex-term.
But it doesn't work for me. Is this even possible?
I have a server with PHP 5.3
/edit:
my final goal is this:
if have a html-page with e. g. following source-code:
HTML
<html>
<head>
<title>Titel</title>
</head>
<body>
<div>
<p>
p-test<br />
br-test
</p>
<div>
<p>
div-p-test
</p>
</div>
</div>
</body>
</html>
And after processing it should look like
$htmlArr = array(
'html' => array(
'head' => array('title' => 'Titel'),
'body' => array(
'div0' => array(
'p0' => 'p-test<br />br-test',
'div1' => array(
'p1' => 'div-p-test'
)
)
)
));
Placeholders in the replacement string use the $1 syntax. In the regex itself they are called backreferences and follow the syntax \1 backslash and number.
http://www.regular-expressions.info/brackets.html
So in your case:
preg_match_all("~<(.*?)>.*?</\\1>~",$string,$matches);
The backslash is doubled here, because in PHP strings the backslash escapes itself. (In particular for double quoted strings, else it would become an ASCII symbol.)

What does <? php echo ("<pre>"); ..... echo("</pre>"); ?> mean?

The question is the tag <pre> </pre>
I've seen one script I am working on, uses it:
echo ("<pre>");
....
....
echo ("</pre>");
What exactly does it do ?
Is it an Html tag or a PHP ?
I've searched on Google but nothing much comes out of it. When do we usually use that HTML tag?...or PHP tag?
The <prev> tag doesn't exist, but it's probably the <pre> HTML tag to put around debug output, to improve readability. It's not a secret PHP hack. :)
I think you're talking about <pre></pre>. element is displayed in a fixed-width font, and it preserves both spaces and line breaks.
try printing an array with a **<pre>** and whitout **<pre>**
$arr = array(1, 2, 3);
echo '<pre>';
print_r($arr);
echo '</pre>';
print_r($arr);
echo (""); is a php code, and <prev> tries to be HTML, but isn't.
As #pekka said, its probably supposed to be <pre>
It is nor php nor html it sounds like specific xml tag.
The PHP function echo() prints out its input to the web server response.
echo("Hello World!");
prints out Hello World! to the web server response.
echo("<prev>");
prints out the tag to the web server response.
echo do not require valid HTML tags. You can use PHP to print XML, images, excel, HTML and so on.
<prev> is not a HTML tag. Is is a valid XML tag, but since I don't know what page you are working in, i cannot tell you what it is. Maybe it is the root tag of a XML page, or a miswritten <pre> tag.
try this:
$names = array('Jesse', 'joey', 'jelnny', 'justine');
$names = new ArrayObject($names);
echo '<pre>';
print_r($names);
vs this:
$names = array('Jesse', 'joey', 'jelnny', 'justine');
$names = new ArrayObject($names);
//echo '<pre>';
print_r($names);
and it shows what the PRE does very neatly
The <prev>-tag might be an XML-tag.
if you put text within an HTML tag, all spaces and line breaks will be preserved.
Otherwise, the default behaviour is to remove multiple spaces and keep only one space, and ignore line breaks (unless you use the tag).
I'd like to say it's from the time before CSS - not necessarily used to make text more legible, only more formatted.
"<pre>" is an HTML tag. If you insert this line of code in your program
echo "<pre>";
then you will enable the viewing of multiple spaces and line endings. Without this, all \n, \r and other end line characters wouldn't have any effect in the browser and wherever you had more than 1 space in the code, the output would be shortened to only 1 space. That's the default HTML. In that case only with <br> you would be able to break the line and go to the next one.
For example,
the code below would be displayed on multiple lines, due to \n line ending specifier.
<?php
echo "<pre>";
printf("<span style='color:#%X%X%X'>Hello</span>\n", 65, 127, 245);
printf("Goodbye");
?>
However the following code, would be displayed in one line only (line endings are disregarded).
<?php
printf("<span style='color:#%X%X%X'>Hello</span>\n", 65, 127, 245);
printf("Goodbye");
?>
The <pre> is used to define pre-formatted text.
The text within <pre> tag is displayed in a fixed-width font and it preserves both spaces and line breaks that are present in the text.Here I'm printing a JSON FILE without <pre> tag and then with <pre> tag.
Without <pre> tag
https://i.stack.imgur.com/ofRn8.jpg
With <pre> tag
https://i.stack.imgur.com/XzDVg.jpg
$testArray = [
[
"name" => "Dinesh Madusanka",
"gender" => "male"
],
[
"name" => "Tharaka Devinda",
"gender" => "male"
],
[
"name" => "Dumidu Ranasinghearachchi",
"gender" => "male"
]
];
print_r($testArray);
echo "<pre>";
print_r($testArray);
here whatever we write in between the pre tags
it will be interpreted same as html pre tag
ex:
<?php
echo '<pre>';
echo '
code here
will be displayed
as it
is
namaste
';
echo "this line get printed in new line";
echo "</pre>";
echo "Now pre ended:";
echo "this line gets joined to above line";
?>
and content b/w 's font also changes.

Categories