Web content manipulation is one of the challeges faced by webmasters. Handling form feeds, different text formats and maniplating HTML tags in content can be tricky. Thanks to PHP which provides you with a variety of text manipulation functions. Using these functions you can achieve almost any desired text formats, handle any undesired situations and build an effective cotent management system for your website. In this article I will explain a few commonly used text manipulation techniques using PHP. It does not obviously cover all the aspects of content management but at least will give you a few very useful ways of performing some very frequently used techniques.
If you are running a PHP website where you allow users to post plain text on the website, you would normally achieve this with a form having a textarea field for the text input from the user. Now suppose one of your users post something like this :
hello, in multiple lines. plz
I am sure you have no problem in inserting the piece of text into your database field. But guess how is it going to be shown on the page when you display it using a SELECT query. Here is how it is shown :
Dear Sir,I would like to have these lines display in multiple lines.Thanks
Now you do not want it to display like this. Lets see what PHP can do for us in this matter. By using nl2br you can insert line breaks in the text very easily. And it does not require any lengthy code either. Suppose you have fetched the text and put it in a variable called $text.
Now here is how you can use the nl2br function to insert line breaks in the text:
$text_with_line_breaks = nl2br($text);
str_replace is another very useful function of PHP. Let us suppose you have a website and a database for Bicycles and you have got many pages written about their types, functions and parts etc. Now somehow you want to make the word "BICYCLE" bold for all its occurances in the text. Guess how hectic it would be to have it done by inserting <b> tags at every single place. By using PHP"s find and replace string function, this can be achieved very easily:
// Let us suppose the $text as a variable holding a text value $text = "Bicycle a cheaper way of travelling short distances. Bicycle is also good for health."; // Now lets put the word "Bicycle" in another variable $boldword = "Bicycle"; // Replace the string using the str_replace function. $new_text = str_replace($boldword, "<b>" . $boldword . "</b>", $text); // The $new_text now contains Bicycle a cheaper way of travelling short distances. Bicycle is also good for health.
It is a common problem which can be a nightmere in circumstances where users are allowed to edit their details online using HTML forms. Suppose you have a form which allows users to change their nicknames:
<input type="text" name="nickname" value="<=$nick_name>"> // Assuming the $nick_name having a value of "Best Coder" //the input field will become <input type="text" name="nickname" value="<Best Coder>">
Now lets suppose the user changes his nickname and write "The"Best"Coder".
Guess what? Having this form posted through the browser, you are either going to have a value of "The" for the nickname field or worst have your page completely disarrayed.
You can face similar problems if some tries to include single quotes, double quotes, ampersand, less than and greater than characters.
The solution with PHP is again easy !!. Use the htmlspecialchars function and pass everything which you thing have the potential to cause any such problem, through it and you are safe. The htmlspecialchars function converts these characters into their equivalent ASCII which are displayed as desired by the browsers.
No comment has been posted for this article yet. Be the first to comment below
When you allow users to post any HTML on the site, make sure you have appropriate measures to handle any hacks. Using cross site scripting (XSS), hackers can put javascript on your website which in turn can cause session hijacks ( Posted by nick )
Post A Comment