Found 1046 Articles for PHP

Strip punctuation with PHP

AmitDiwan
Updated on 09-Apr-2020 10:59:31

789 Views

The ‘preg_replace’ function can be used to match the characters in the string and remove the unnecessary characters.To keep letters and numbers −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am 8 yearsTo keep letters only −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am yearsTo keep letters, numbers and underscoreExampleOutputThis will produce the following output −Hello my name is Bobby I am 8 years

Detect base64 encoding in PHP?

AmitDiwan
Updated on 09-Apr-2020 10:49:18

488 Views

To detect base64 encoding in PHP, the code is as follows −Example Live Demo

How can I remove all empty values when I explode a string using PHP?

AmitDiwan
Updated on 09-Apr-2020 10:48:09

1K+ Views

The array_filter() or PREG_SPLIT_NO_EMPTY option on preg_split() can be used to remove empty values from a string when it is exploded −Example Live Demo

PHP: fopen to create folders

AmitDiwan
Updated on 09-Apr-2020 10:44:09

1K+ Views

The fopen can’t be used to create directories. This is because fopen function doesn't create or open folders, it only works with files.Before using the fopen function, one should check with is_dir first if it exists, if not create it using the mkdir function −$filename = '/path/to /file.txt'; $dirname = dirname($filename); if (!is_dir($dirname)) {    mkdir($dirname, 0755, true); }The above code creates a path to the file named ‘filename’. The directory of the ‘filename’ is obtained using the ‘dirname’ function. Next, this directory is checked for the existence using the ‘is_dir’ function. If the directory already exists, no operation takes ... Read More

Check if a PHP cookie exists and if not set its value

AmitDiwan
Updated on 09-Apr-2020 13:51:39

1K+ Views

Based on the PHP manual, the existence of a cookie can’t be found.A reference from the manual: “Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.”The reason being cookies are response headers to the browser and the browser needs to then send them back along with the next request. This is the reason they are available on the second page load only.But here is a work around for the same: The $_COOKIE can be set when the setcookie function is called −if(!isset($_COOKIE['lg'])) {    setcookie('lg', 'ro');    $_COOKIE['lg'] ... Read More

How to import csv file in PHP?

AmitDiwan
Updated on 09-Apr-2020 10:40:30

1K+ Views

The below code can be used to import CSV file in PHP −The file will display the contents of the csv file are displayed on the screen.In the above code, beginning from row 1 (since row 0 would usually contain headers/column names), the csv file is opened in read mode and the fgetcsv function is called to read in 1000 rows of data present in the csv file.The number of columns in every row is displayed along with the contents of it.

PHP using scandir() to find folders in a directory

AmitDiwan
Updated on 09-Apr-2020 10:36:44

12K+ Views

To check if a folder or a file is in use, the function is_dir() or is_file() can be used.The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.For example$scan = scandir('myFolder'); foreach($scan as $file) {    if (!is_dir("myFolder/$file")) {       echo $file.'';    } }OutputList of files and directories inside the path specified (if any)The directory ‘myFolder’ is scanned using the ‘scandir’ function and the files and directories inside it are listed out. The ‘foreach’ ... Read More

How do you make a string in PHP with a backslash in it?

AmitDiwan
Updated on 09-Apr-2020 10:35:14

319 Views

When the backslash \ does not escape the terminating quote of the string or even create a valid escape sequence (in double quoted strings), then the below code can be used to produce one backslash −Example Live Demo$string = 'abc\def'; print($string);OutputThis will produce the following output −abc\defExample Live Demo$string = "abc\def"; print($string);OutputThis will produce the following output −abc\def

How to validate domain name in PHP?

AmitDiwan
Updated on 07-Apr-2020 13:21:09

1K+ Views

The domain name can be validated using the below code in PHP −Example Live Demo $domain_name = 'https://tutorialspoint.com' is_valid_domain_name($domain_name)OutputThis will produce the following output −$domain_name = 'https://tutorialspoint.com' is_valid_domain_name($domain_name)In the above code, the ‘preg_match’ function is used to match the domain_name passed as an argument to the user-defined function ‘is_valid_domain_name’.

How to install Imagick/imagemagick PHP extension on Windows 10?

AmitDiwan
Updated on 07-Apr-2020 13:18:50

222 Views

To install Imagick or Imagemagick on windows, follow the below mentioned procedure −Check the permissions on the .dll file. This will make sure that the Apache user has read access to the file.It is better to change the permission of the [PHP]/extension directory.In order to change the permission, follow the below steps −Right click on the file(s) or folder(s)Select "Properties"Select the "Security" tabClick on "Edit" button.Change the permission of user to Full Control.

Advertisements