2009

PHP String Case: Uppercase all Letters or Lowercase all Letters in a String | Uppercase First Letter of a String | Uppercase First Letter of all Words in a String

It’s common that you need to reformat the texts in PHP and make them look a little more formal. For example, the first letter of all sentences should be uppercased, or with a title, the first letter of all words should also be made uppercase; in other situations, you may want all letters to be …

PHP String Case: Uppercase all Letters or Lowercase all Letters in a String | Uppercase First Letter of a String | Uppercase First Letter of all Words in a String Read More »

PHP: Change Tabs to Spaces and Vice Versa in String | Change Tab Sizes / Lengths in Strings

Everyone who ever dealt with formatting code in PHP string manipulations may want to switch between different tab sizes or simply change spaces to tabs or tabs to spaces in the formatted code string. The function we’ll need for these is str_replace(), the string search and replace php function. For example: Switch between tabs and …

PHP: Change Tabs to Spaces and Vice Versa in String | Change Tab Sizes / Lengths in Strings Read More »

PHP: Check if a string contains another string or substring

As a web site templating language, string processing is what PHP is good at. There’s always a chance that you need to verify and make sure if a string contains another string. For example, to check if an at sign is included in the entered email address by a visitor: if (strpos($_POST[’email’], ‘@’) !== false) …

PHP: Check if a string contains another string or substring Read More »

PHP: How to access a global variable inside a function without passing it?

Ideally, global variables outside a function can not and should not be accessed from within the function to hold up the programming modularization paradigm. However, if you really need to do this, especially in small programs, you can declare the variable again inside the function by the keyword ‘global‘ so as to use inside the …

PHP: How to access a global variable inside a function without passing it? Read More »

PHP cURL: Fetching URL and Sending Request with Cookies

One of the things the remote web server inspects is the client cookie to know about the requester. If you need cURL to simulate a user browser that sends cookie information to the web server, you need the following options: $c = curl_init(‘http://www.example.com/needs-cookies.php’); curl_setopt ($c, CURLOPT_COOKIE, ‘user=ellen; activity=swimming’); curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec …

PHP cURL: Fetching URL and Sending Request with Cookies Read More »

PHP cURL: Making POST Request to URL

By default, all HTTP requests made out by cURL is GET requests that simply fetches the URL and don’t submit any more POST variables. However, if you need to fetch and retrieve URL by the POST method with cURL, you need the snippet below: $url = ‘http://www.example.com/submit.php’; // The submitted form data, encoded as query-string-style …

PHP cURL: Making POST Request to URL Read More »

PHP: GD Library Drawing Functions Reference

To draw a line, use ImageLine( ): ImageLine($image, $x1, $y1, $x2, $y2, $color); To draw an open rectangle, use ImageRectangle( ): ImageRectangle($image, $x1, $y1, $x2, $y2, $color); To draw a solid rectangle, use ImageFilledRectangle( ): ImageFilledRectangle($image, $x1, $y1, $x2, $y2, $color); To draw an open polygon, use ImagePolygon( ): $points = array($x1, $y1, $x2, $y2, …

PHP: GD Library Drawing Functions Reference Read More »

PHP: Change Error Reporting Level | Different PHP Error Types

Debugging a PHP script, you may want to alter the error-displaying sensitivity on a particular page and control what types of errors should be reported. The solution is the PHP error reporting function error_reporting(): error_reporting(E_ALL); // everything error_reporting(E_ERROR | E_PARSE); // only major problems error_reporting(E_ALL & ~E_NOTICE); // everything but notices As the parameters of …

PHP: Change Error Reporting Level | Different PHP Error Types Read More »

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File

Of course, you will have to read in the file first, preferably in an array. To read a file in an array, you just need the file() function: $lines = file(‘quotes.txt’); Then, you shuffle the array with shuffle() function that randomizes all the items in the array thus shuffling the lines in the file quotes.txt: …

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File Read More »

PHP: Count Lines of a File and Get the Number of Lines in a Text File

As we know, lines are separated from each other by line breaks indicated by “\n” in PHP, therefore, one method would be to explode() the entire file content string by “\n” into an array and then count() the number of the array items to come to the number of lines of that file. However, there’s …

PHP: Count Lines of a File and Get the Number of Lines in a Text File Read More »

PHP: Reading a File into a String or Reading the File into an Array

Aside from the common approach in reading a file with php fopen() function which is a little annoying in that it requires extra steps before actually reading the contents of the file. PHP has other file reading functions to enable you instantly get what you want and read the contents of the file without the …

PHP: Reading a File into a String or Reading the File into an Array Read More »

PHP: Make or Create A Directory

The function in PHP that’s used to create a directory is just the same with that in Linux bash shell: mkdir. First make sure the current working directory is where you want the new directory to be, then run this line of PHP: mkdir(‘new_dir’); // creates a new directory named new_dir under the current directory

Scroll to Top