I am developing a Facebook App in which I get some text from the Database and Display it on the Page.
The Problem is that I want to insert a line Break in the variable e-g
If I copy a Text from database and store it in a Variable..
Let say
$text="I love to walk";
I want to insert a line break after "to" how can i do that?
I had tried to store the text like this in html
"I love to <html> <br> </html> but that didn't worked..
Just suppose this is the Text ..may be next time the text is entirely Differnet having no "to" word.
Depends on if you want to create new line in code output, or in HTML
$nl = "\r\n";
$nl_html = "<br />";
That exmple you provided modify like this:
$lyrics = "I love to <br> but that didn't worked.."
To automatically add line break after some text, use preg_replace
$lyrics = preg_replace('/to /',"to<br />",$lyrics);
see http://php.net/manual/en/function.preg-replace.php
$new_str = str_replace('to', 'to <br />', $str, 1);
If you want to output the text in a html page, you need to make it
$text="I love to <br /> walk";
If you want to output it to a file you need to make it
$text="I love to\r\nwalk";
or
$text="I love to\rwalk"; depending on the OS on which you will be reading the file
Hi I was having issues with this just as you do earlier today (I am new to PHP).
The way I fixed this was as follows:
$format_text = nl2br($formatthis);
You would then refer to $format_text.
What it does is it keeps the line breaks.
However I am not quite sure what you mean with your OP, after re-reading it. I went by the topic and I answered it as best I could.
If you are having trouble let's say echoing html code then most definitely you are having trouble with escaping characters.
For instance:
echo "a href="something" /a this won't work.
echo "a href=\"something\" /a this will work, notice the .
you have two options either use preg_replace or use a variable to save the value
please see php documentation for further info
Related
Pulling in data from a Filemaker Pro database field and trying to convert the plain text data from the field into a clickable link to google maps via PHP.
My first attempt doesnt display anything when called:
$Venue = '';
$Venue is then echoed into a UL via
<?php echo $Venue; ?>
I'm relatively new to PHP so I'm sure there is a much more semantic way of marking this up? Possibly a regex and replace, returning a preg_replace? Which is what I've been using for plain text URLs and email addresses.
Anything helps, thanks so much.
Casey - not sure there's enough context here to help you? For instance, forget the link wrapping, does
<?php echo $record->getField( 'Auctions::AIS_Venue'); ?>
actually echo the field contents to a page?
Also, not sure you really want to use nl2br anyway, as you probably don't want a break in your url ;-)
OK, in which case there are a couple of things to try. First - can you get it to run in a webviewer inside FileMaker? I suspect not, and it may be down to an encoding/filtering issue.
There are two approaches you could use.
Create a new calc field in FileMaker that filters out punctuation and converts spaces to '+'s, something like:
Filter( Substitute( Lower( Auctions::AIS_Venue ) ; [ ΒΆ ; "+"] ; [" " ; "+"]; ["++" ; "+"] ); "abcdefghijklmnopqrstuvwxyz1234567890+" )
and then use that field as in your PHP code, or;
Do the same calculation on the fly in PHP, something like:
$Venue = '';
That should do the trick ? ;-)
I am not able to figure out how to get an HTML out of a simple quoted_pritable_decode command.
Consider the following code from w3schools.
$str = "Hello=0Aworld.";
echo quoted_printable_decode($str);
The output for this code is:
Hello world.
However, when I view the source it is:
Hello
world.
Well, I am trying to figure out if there is a simpler way to get the second output. I would like to store it in my database in HTML format. I am aware of way arounds like replacing the encoded part, however, is there a recommended way here?
Edit 1:
Both the answers are perfectly fine and a precise explanation from Xavier was great. Since, I just have to mark one as correct, I mark the one which I believe is more precise.
You could use nl2br() encapsulating that when you are storing it into your table.
<?php
$str = "Hello=0Aworld.";
echo nl2br(quoted_printable_decode($str));
Change your code to:
$str = "Hello=0Aworld.";
echo "<pre>" . quoted_printable_decode($str) . "</pre>";
What you echo is being interpreted as pure HTML, and HTML doesn't honor newlines, except when you use tag. You also could do a echo str_replace('\n','<br>',quoted_printable_decode($str)); (i.e. AFTER decoding)
I'm quite new here. I'm trying to make a blog/journal site that allows users to post their own journal. I'm still quite reluctant on making it because I am really afraid of malicious code injections.
So here's a sample code:
<?php
$test = "<b>blah</b>"; //User input from SQL
echo "$test";
?>
What will come out is just the word "blah" in bold right? What I was trying to achieve was to echo "<b>blah</b>" instead. I don't want people to put some PHP codes that can actually mess up my whole web page. Please keep in mind that the variable $test is actually a MYSQL query, so that variable will be needed as an example. I know you can do echo '$test'; but it just comes out as "$test" instead. I feel like pulling my hair out I can't figure it out yet.
The second solution I know of is the htmlspecialchars(); function, but I want the strings to display as what I typed, not the converted ones...
Is there any way I can do that?
I think the OP wants the HTML itself to be output to the page, and not have the tags stripped. To achieve this, you can run the string first through htmlentities()
$test = '<b>blah</b>';
echo htmlentities($test);
This will output:
<b>blah</b>
Which will render in the page as
<b>blah</b>
Echo don't execute PHP code from string. This is impossible and this is not security hole in your code.
You can use a template engine like Twig for exemple.
If htmlspecialchars(); is not the one you are looking for, try the header() option.
header('Content-type: text/plain');
When you are gonna give <b>Hi</b> to a browser, it will be displayed in Bold and not the text be returned. But you can try this way, outputting it inside a <textarea></textarea>.
Or the other way is to use htmlentities():
<?php
$test = "<b>blah</b>"; //User input from SQL
echo htmlentities("$test");
?>
I am having trouble with "\n" character not working. I realized that it wasn't work while testing output of variables using a simple echo statement. I have tried approaching the new line character a few different ways to see if it was just me, but nothing I have tried is working. Here is an example of some attempts I have made:
<?php
// Establish Connection to Taskaro DB
require "../_connections/connection_taskDB.php";
// Start Session
session_start();
// Create Session Variables
$_SESSION['userID'];
$_SESSION['companyID'];
$_SESSION['usernameDB'];
// Convert Session Variables to page variables
$userID = $_SESSION['userID'];
$currentUser = $_SESSION['usernameDB'];
$editType = $_REQUEST['editType'];
$projectID = $_REQUEST['projectID'];
// Testing if new line character is working
echo "hello, Mr. New Line!\n\r";
echo "This line should be below 'hello, Mr. New Line!'";
// Testing variable and session connection
echo "SESSION VARIABLES:"."\\n\n"."userID = {$userID}";
echo "userID = {$userID}"."/n";
echo "currentUser = {$currentUser}"."\r";
echo "companyID = {$companyID}\n\r";
echo "\nPOST VARIABLES:\n";
echo "editType = {$editType}\n";
echo "projectID = {$projectID}\n";
?>
I read up on some other overflow questions that had similar problems and none of them fixed my problem. The project is on a remote server (GoDaddy) in which php has been installed. The document has the correct file extension (.php). I am coding in dreamweaver and uploading my script for testing. From the code you can see I've tried "\n","\n\r","\r". I've also tested in both Firefox and Google Chrome.
I also tried to concatenate the "\n" character, and took a shot in the dark and even tried using the forward slash rather than the backslash (I knew it wouldn't work, but I'm getting pretty frustrated at this point). I bet it's something simple but I don't see what else is could be. Thanks in advanced.
If you view the source of the page, you will see all of those values output on separate lines.
If you are viewing the file in the browser, you need to use line breaks (<br />) if you want your text to show up on different lines. HTML ignores newlines in regards to presentation.
echo "hello, Mr. New Line!\n";
echo "This line should be below 'hello, Mr. New Line!'";
When viewing source, the above two text strings will be on separate lines. When viewed in the browser they will appear to be on the same line.
echo "hello, Mr. New Line!\n<br />";
echo "This line should be below 'hello, Mr. New Line!'";
When viewing source, the above two text strings will be on separate lines because of the \n. When viewed in the browser they will also be on separate lines because of the HTML break <br />.
Use the PHP_EOL constant instead of \n and call it a day.
Also, it's \r\n, not the other way around.
If you are expecting the browser to render new line characters as new lines in HTML, that won't happen. You need to use the <br> tag.
I'm trying to experiment with taking text from a textarea in a flex project and open it up in a php page. But the php isn't line breaking where it should be...
an Example of the text i'd like to bring over to php would be:
You: Hi there
Them: Hello
You: This is a great example
Them: I know right?
Here's my php:
<?php
$text= $_GET['text'];
echo $text;
?>
Right now I came up with something like this in the actionscript...
var chatBox:String=chat_box.text;
navigateToURL(new URLRequest("savelog.php?text="+chatBox), '_blank');
I also tried something like:
var chatBox:String=chat_box.text.valueOf().replace("\n","<br/>");
and
var chatBox:String=chat_box.text.toString().valueOf().replace("\n","<br/>");
But apparently the \n isn't translating over no matter how I get the chatBox var so its not even making a <br/>
But, even if i did get that to work it wouldnt be ideal. Because eventually in the end I want to be able to just incorporate the pastebin API to paste this GET data and post it on there. And I don't think it would look too pretty with having <br/> after every line...
So my questions is, is it possible to bring this text over to php and recognize the line breaks in a way that would work well with what im eventually trying to accomplish?
edit:
I'm a little confused because var chatBox:String=chat_box.text.toString() actually returns the text with \n at every line break and i can see the \n on a trace statement...but when im looking in the URL text there is no \n anywhere...any ideas? because if the \n would appear the ln2br() solution might work
Take a look at ln2br() its a built in function in PHP. It also handle more than jsut the \n might be usefull in your case and i dont see that your using it yet! Try it out and let me know! You also might want to check out any encoding issues.