Skip to main content

Posts

Showing posts with the label PHP functions

br2nl() - PHP custom function to convert BR tags in a string to Newline Character

Line Break to new Line character conversion in PHP When we fetch string data which contains BR tags from database to display in a textarea, the BR tags may display as such with out adding a new line. To handle this we have to convert the BR tags in the string to Newline character. Here is a custom php function to do this function. This function works just opposite to nl2br() builtin function in PHP which converts all New line character to BR tag.

PHP function to get dates between two dates in an array

Function in PHP to get dates between 2 dates in an array Here is a simple function in PHP which accepts FromDate and ToDate as input dates. The dates should be in dd/mm/yyyy format. Function takes the input dates and returns an inclusive array of the dates between the from and to dates.

How to prevent Cross Site Scripting ( XSS ) attack using .htaccess file and PHP

XSS attack prevention using .htaccess file and PHP. Cross Site Scripting of XSS attack is a type of Cyber attack in which hacker is able to include malicious JS or iframe codes into the webpages by expoiting the vulberabilities in the web page url. If successful, the hacker can manipulate or steal cookies, create requests which appear to come from a valid user, compromise confidential information, or execute malicious code on end user systems. Hacker can include JS or iframe codes as parameters of Query string variable. if the REQUEST variables are not validated and if it is printed on the page as such, then the page content will contain the malicious script embeded in that. The effect of XSS attack may range from a petty nuisance to a significant security risk, depending on the sensitivity of the data handled by the affected site.

PHP function to Remove Non AlphaNumeric Characters from a given string with exclusion list

PHP provides various pattern matching regular expression functions as well as string functions to perform this task. PHP built-in functions for this are str_replace, str_ireplace, preg_replace, substr_replace ,preg_match It is recommended to use PHP string replace functions over regex functions to do simple string replace functions.There is nothing wrong with using a regex , regular expressions have to be compiled and strings to be parsed, but some people wrongly them by doing silly things like : preg_match('/txt/', $str) instead of strpos('txt', $str) !== false      OR preg_replace("/txt/", "", $str) instead of str_replace('txt', $str)

Is there any MySQL function to return date from unix time stamp?

In the DB table, when a record is inserted the timestamp is also inserted. This time stamp is added in the insert query using the time() function. When The records are listed currently the php date( $timestamp , "d/m/Y h:i:s" ) function is used to dhow the data back. Is there any function in MYSQL which can  generate date from the unix timestamp stored in the table?

How to get First and last time stamp value of a particular date in PHP

PHP custom function to get  First and last time stamp value of a particular date. To generate unix time from a date we use mktime() function in php Syntax of the function is given below: mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] ) **$is_dst means "is DST ( Daylight Saving time) applied

PHP Function to remove the HTML tags along with their contents - PHP function to remove HTML tags only

Usually strip_tags() function is used for removing tags from an html string. but there are some issues with this function 1. It does not validate the HTML, partial or broken tags can result in the removal of more text/data than expected. 2. It does not modify any attributes on the tags that you provide as allowable_tags parameter. 3. It may give different outputs for different versions of the same tag. For example for <br> and <br /> 4. For a badly formated HTML string like " PHP guys <b<b>> rocks </b<b>> ", it may give unexpected results.

PHP function to Create unique Order Number or Sequence number

PHP function to Create unique Order Number There may be some situations in which a unique order number or sequence number required to identify a transaction. Instead of depending on the Auto increment fields of the table , programmers may use some sequence number generating functions.

How to check mod_rewrite module is enabled using PHP script?

Generally what programmers do is uploading a test.php file to the server. The file will contain  the below code <?php phpinfo(); ?> But this won't work if you want to check whether Your webserver's mod_rewrite is enabled or not. mod_rewrite is not a PHP module , it is an extension of your webserver (Apache / IIS ), So  It won't be visible in phpinfo(). In some servers you can see this in Apache info section of listing. It may be visible in the loaded modules section. This article discuss varies tips to check whether the rewrite module is enabled or not and also it give an idea about how to enable it.

PHP function to return the domain or subdomain from a given url

PHP code to return the domain name from a given url string Here is a simple function which uses regular expression to find the domain name from a given url string. Input to the function is the URL string.

PHP script to list files in a given directory using opendir, readdir and glob functions

PHP script to list files in a given directory The below given script goes through a given directory and list the name of the files in the order as it is stored in the file system. The first script uses a directory handler to operate on a given directory. It opens the handler, reads file entries one by one by looping through the directory (during this step you can process the file ) and finally close the handler

PHP Dynamic Image generation script - create fast loading image gallaries

PHP Dynamic Image generation script A simple script in PHP which generates less sized image on runtime. the script needs PHP GD library installed on the server. This will save the page loading time A large image with huge sized image can be scaled and MB size of image can be reduced Script can generate image in Large,medium,small and custom size This can be used to create simple PHP image gallaries Image name should be passed as base64 encoded

Simple PHP function to generate alphanumeric password string

Here is a simple PHP function, that can be used for generating strong passwords. # The generated string will contain Numbers, Special characters and Upper & Lower case alphabets # You can specify the number of characters in the generated string # If number of characters is not given, the function will return an alphanumeric string having 7 characters with all type of characters given above # Function call with string length generatePasswordString(8) # Function call with out string length generatePasswordString()

Paymentech Error in form submission x_fp_hash Could not validate the integrity of the payment

For those who use Chase paymentech hosted payment gateway might have gone through a rare error senario. Here the user was shown a warning message and an email was also sent to user with error details. ------------- Error in form submission An error page was displayed to the customer. x_fp_hash : Could not validate the integrity of the payment from the transaction -------------

French characters are not properly displayed in PDF created using PHP FPDF class

Problem in displaying French characters or other language characters that uses latin scripts  on PDF files generated using FPDF class I am using FPDF class Version 1.6 to generate a pdf file which have french characters in it. But on the PDF file the french characters are either discarded or displayed as some other unfamiliar characters. Solution: I tried the below given script to fix the issue. =============== function change_encoding($str) { return ( iconv("UTF-8", "CP1250//TRANSLIT", $str) ); } =============== The function given above will accept a string and generate the french character friendly encded string. string iconv ( string $in_charset , string $out_charset , string $str ) the function iconv converts a string to requested character encoding. It performs a character set conversion on the string "$str" from "in_charset" to "out_charset". Use of TRANSLIT and IGNORE Appending "//TRANSLIT...

How to fix the problem in displaying french accented characters from Mysql table on a PHP page?

In a php page I am displaying some hard coded french  texts which has some accented characters like "é è ..." and also some french characters are fetched from Mysql table and displayed on the page. Here the problem was the hard coded french characters were displaying fine, but the characters fetch from db was not showing properly. It was displaying like "?" with in a black background.

PHP Session is not working for my website - How to fix?

I had a website which uses a login page for registered users. It uses the $_SESSION variable for string the current session values after a user succesfully logins. This was working fine, but on a sudden it stopped working. When the user submit login info, it doesnt give an invalid user message or any other warnings ,but return back to login page. Some of you guys ,might have gone through the above mentioned scenario while developing / managing your website. The possible reasons are mentioned below.

How to check the file type of an uploaded file in PHP - Checking of extensions and mime type of an uploaded file

How to check the file type of an uploaded file in PHP In some web applications, there are situations in which users should only be allowed to upload certain type of files to server. For example, the application may accept only JPG type of files. Whne we upload a file, it is taken to a temperory location on server and from there it is copied to actual location on server. In php script When we post a from which is having a file control, the selected file content will be available in $_FILES variable. // in the form the control would be named as document_file <input type="file" name="document_file"....... On submission we have file content in $_FILE["document_file"] and it is placed in a temperory location on server, once the move_uploaded_file function is called, this file is moved to actual location on server. So before calling the move_uploaded_file function, we can check the type of the file and confirm whether it is allowed file type or not...

TCPDF Fatal Error Allowed memory size exhausted - How to fix?

Fix for TCPDF Fatal Error "Allowed memory size exhausted" When using TCPDF to generate a PDF file by keeping a large sized images as background image of PDF I got a Fatal error. it printed the following fatal error message Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9920 bytes) in D:\wamp\www\tcpdf\unicode_data.php on line 4835 This means that the process is trying to consume more memory that is allowed. The PHP config file php.ini specifies how much memory each script is allowed to use for the execution. May be the allowed Mb of RAM(memory) isn't enough for PHP script, that generate PDF file.

How to replace French accented characters with English character equivalents using php?

Script for replacing french accented characters with English character equivalents during the generation of SEO urls Usually we build SEO friendly urls by replacing the French characters with their English equivalent. I had tried several ways to do this, but failed. So just created a simple small function for this. The function simply does the following: First it generates 2 arrays, One array will hold the required urlencoded equivalents of the french accented charactes and second array will hold the English characters. Next step is to generate a url encoded string ofcharacters from the french string Next, the function will replace the urlencoded equivalents in the string with corresponding English character. Function is given below


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog