Author Archives: Andrei

Remove html tags and malicious content in php

malicious

Remove html tags and malicious content in php Let’s say you have some data that are coming from the client side (a form submit, an ajax call, etc) and you want to be sure that you are not inserting in the database malformed data with html tags or other stuff. In the end you would need

How to save image from URL using PHP

snippets

How to save image from URL using PHP You have an image in a URL, something like “http://www.yourhowto.net/wp-content/uploads/2012/09/mysql-java-jdbc1.jpg“, and you want to save that image in your computer, but you want to obtain that directly from PHP. You can use 2 methods for this, one is using CURL and one using fopen directly. First method

How to generate random string in PHP

snippets

How to generate random string in PHP Have you ever needed to generate a random password from a given set of chars in PHP? You could also probably wanted a password salt used for encrypting the user password. Well here are two solutions which I can think of at the moment to help you generate

How to get last key in an array in PHP

snippets

How to get last key in an array in PHP? Arrays can have lots of elements, some that may not be needed at one point. Let’s say that we need in some cases the last key or last element of an array in PHP, what would you do? A very good solution and clever practice

How to check if URL exists in PHP?

snippets

How to check if URL exists in PHP? Let’s say you want to check if an URL exists. You want to do that with PHP because that is what you know, and you want to perform some savings into the database if that URL exists. This can be done in various ways which will work

How to add element to array PHP

snippets

How to add element to array PHP If you are a programmer, you will need to work with arrays at some point, arrays are something referred to like a collection of elements. It is much easier to have an array with n number of elements, instead of having n number of variables. Problem Lets say

How to remove the last character in a string in PHP ?

snippets

How to remove the last character in a string in PHP ? You have a string, let’s say “1,2,3,4,5,6,7,8,” that is generated in a loop, and you want to remove the last comma that is occurred on the last position of the string, to obtain a string like this: “1,2,3,4,5,6,7,8”. 1st solution

2nd

PHP explode equivalent for javascript

snippets

PHP explode equivalent for javascript Very often you have to perform operations on strings to split them, to obtain only a desired piece of it. If the string is formatted in a way that allows you to split, eg. you know the delimiters, you can use the following way to do this. Problem Let’s say

PHP math operations and operators

math

PHP math operations and operators PHP is a very powerful language that can help you to perform mathematical operations really easy. Math expressions are made up of operators and operands. You can perform easy operations like adding, subtracting, multiplying or dividing with PHP. Even comparison operators can be used with this language (greater than, less

Convert variable to string in PHP

snippets

Convert variable to string in PHP PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Sometimes you will want to transform a given variable into a string format and obtain a similar output with “ToString()” method in Java or .NET. This could be done easily with one of the following

Request an article ←