PHP Function to replace all new line characters in a string with BR tag

There is an incredible but little known built-in function with PHP that replaces all new line characters within a string with html <br /> tags. Its perfect if you ever want to display the contents of a text area within HTML.

$str = nl2br($str);

This will replace all new line characters with valid XHTML <br /> tags.

There is also an optional parameter to turn of XHTML and just use traditional <br> tags.

$str = nl2br($str, false)

Setting up WordPress on EC2 in 10 minutes

This tutorial does a complete walk-through on the basis of you using Mac Os to set up a new EC2 WordPress site. Once you get practiced, you can do this in minutes!

Log in to your EC2 Management Console (https://aws.amazon.com/ec2/)

Select a new  instance of Amazon Linux AMI (Free tier eligible)

Screen Shot 2015-11-19 at 13.35.35

Just leave everything un-changed on the next screen and press on the Next: Configure Instance Details button (NOT the Review and launch button)

Leave everything on the next screen un-changed too, and press Next: Add Storage

Again, don’t change anything and press Next Tag Instance

Give your new Server a name (name of the wordpress site/company?) then press Next: Configure Security Group

Add two new rules to allow http and https through the firewall

Screen Shot 2015-11-19 at 13.38.50

Click on the blue Review and Launch button

Press on Launch

Create a new key pair and give it a name (something easy to type)

Screen Shot 2015-11-19 at 13.40.19

download the Key Pair File file (from this point onwards referred to as nameOfKeyPair.pem)

remove the .txt extension

Open terminal and navigate to downloads folder (usually cd /users/yourYourName/Downloads/ )

chmod 600 nameOfKeyPair.pem

ssh-keygen -f nameOfKeyPair.pem -e -m per > nameOfKeyPair.pub

chmod 400 nameOfKeyPair.pem

ssh -i name.pem [email protected]

You will be connected to the new instance

Type the following, pressing Enter after every line

sudo yum update

sudo su

yum install httpd

service httpd start

yum install php php-mysql

yum install mysql-server

service mysqld start

mysqladmin -uroot create myblog

mysql_secure_installation

Press Enter (no existing password)
Enter new password (keep a note of this, you need to enter it into the wordpress config in a couple of steps time)
y
y
y
y

cd /var/www/html

wget http://wordpress.org/latest.tar.gz

tar -xzvf latest.tar.gz

mv wordpress myblog

cd myblog

mv wp-config-sample.php wp-config.php

vi wp-config.php

—press i for insert—

Make the following changes to the wordpress config file

Database: myblog
user: root
password: what you set it as during the security setup previously

press Esc

press wq to exit editor and save the changes

service https restart

browse to the new site and start configuring by typing http://yourinstanceipaddress/myblog

PHP write to screen without using echo

I have been slowly learning PHP for a while now, and whilst I still prefer ASP.Net with its nicely organised code-behind pages and button click methods, there is something very nice about just integrating code and html.

One of the annoyances I had was that in order to (for example) display a variable on the screen within my html, I had to type:

<p><?php echo $VariableName; ?></p>

..but there is an easier way that I have just discovered and thought I would share:

<p><?=$VariableName?></p>

….no semi-colons, no <?php, no echo! Much faster to type!

Obviously you dont need the <p> and </p> tags, they are just there to illustrate that it can be used within HTML.

 

I’ve used this lots in a new website that I have been working on in my free time: http://www.chriswilsonartist.com