Category Archives: Php Frequently Asked Questions

Php replace iframe by amp-iframe


Php replace iframe by amp-iframe It is very simple and easy to replace normal iframe to amp-iframe in Php. here in this article we are going to explain how to convert iframe tag to amp iframe.


Php replace iframe by amp-iframe Example

You can replace iframe tag by amp-iframe tag simply using the below code-

Php replace iframe by amp-iframe Example:

 ";
$resultTemp = str_replace('', '', $resultTemp);
?>

The above code will convert all occurrence of iframe to google amp iframe tag.

Php Replace Img tag by amp-img


Php Replace Img tag by amp-img– We can use regular expression and str_replace to replace image tag by amp image tag.


Php Replace Img tag by amp-img | AMP image tag Example

You can use the below example to replace image tag by amp-img tag-

Php Replace Img tag by amp-img | Example:

. Learn more";
$result = preg_replace('/(]+>(?:<\/img>)?)/i', '$1', $content);
echo str_replace('

The above example will take care about height width and alt tag of amp-img so you do’nt need to worry about anything.

Example 2 Using preg_replace

echo htmlentities(preg_replace(
    '//', 
    '', 
    '
hi bro ' ));

Php htmlspecialchars returning empty string


Php htmlspecialchars returning empty string– Sometimes php htmlspecialchars function returns null(empty) string, this issue may be due some character unicode. I was facing the same issue and i passed utf-8 and ENT_QUOTES parameters and it worked for me.


Php htmlspecialchars returning empty string | Null String

To fix this issue you can pass the second and third parameters in htmlspecialchars function simply as below-

Php htmlspecialchars not working :

htmlspecialchars($string, ENT_QUOTES, 'utf-8')

After passing these parameters it fixed my issue and worked perfectly as expected.

Add Watermark On Image in PHP


Add Watermark On Image in PHP- It is very easy and simple to add watermarks on images in PHP. GD library and GD functions are used for adding watermarks on image in PHP. Here in this tutorial we are going to cover two things – 1. Add Text as watermark. 2. Add Image as Watermark.


How to Add Watermark On Image in PHP?

You can add the watermark on images in following ways –

Add Text As Watermark

Three functions are used to add the text as watermark in php –

  • imagecreatetruecolor– Returns an image identifier representing a black image of the specified size.
  • imagecopy– Used to copy image.
  • imagettftext– This is used to write the texts to the image using TrueType fonts.

Syntax

Here is the syntax for adding the text watermark on image.

Text Watermark : imagettftext() Syntax


imagettftext() function is main function which is responsible for adding the text watermark on the image. This function accepts the following parameters-

  • $imageResource– This is obtained from the function imagecreatetruecolor().
  • $fontsize– This is used to define the font size of the watermark text.
  • $waterMarkAngle– This is used to define the watermark angle.
  • $x, $y– $x is used to define the distance of the watermark from X axis. $y is used to define the distance from Y axis.
  • $fontFilePath– This is used to add the font file path. If font file is not there then download the font file – Download Arial Font
  • $watermarkText– Watermark text to be added.
  • $color– This is used to add the watermark font color.

Text Watermark On Image : Example 1 –

Let us pass the font color white, angle 0 degree and font size 30px.

Add Text As Watermark On Image Example:


Try it »

If you run the above example it will produce the output something like this –

Add Watermark On Image in PHP

Text Watermark on Image : Example 2 –

Let us pass the font color white, angle 45 degree and font size 40px.

Add Text As Watermark On Image PHP Script Example:


Try it »

If you run the above example it will produce the output something like this –

Add Watermark On Image diagonal php

Add image as Watermark

Apart from texts you can also add image as watermarks. Sometimes we need some image to be used as watermark such as logo of company on each images. Php imagecopy() function is enough to add image watermarks on another image. You can add the image watermarks simply as below –

Syntax

Syntax for image watermark is as –

Text Watermark : imagettftext() Syntax


The above function is enough for adding the image watermark on another image.
The imagecopy function copy the source Image($srcImage) onto the destination image($dstImage). Input parameter of imagecopy() function are as –

  • $dstImage– The main image on which you want to add the watermark.
  • $srcImage– The image used for watermark.
  • $dstX, $dstY– The x, y co-ordinates of destination image.
  • $srcX , $srcY– The x, y co-ordinates of the source image.
  • $srcW , $srcH– The height & width of the image to be copied. The image will be copied with a height of $srcW & Height of $srcH from the co-ordinates $srcX, $srcW And will be added onto the destination image’s co-ordinate $x, $y.

Add Image Watermark : Example –

Let us add watermark simply using the imagecopy() function as below- .

Add image As Watermark On Image PHP Script Example:


Try it »

The above example will add the source image onto the destination image. If you run the above example it will produce the output something like this –

Php Delete an element from array


Php Delete an element from array – You can remove array element using the unset($position) function. It deletes the element at the specified position. Here in this tutorial we are going to explain how you can delete an element form an array. We will explain this with example and demo.


Php Delete an element from array

You can remove an element from array using the unset function as below –

Php Delete an element from array Example:


Try it »

If you run the above example it will produce output something like this-

Php Delete an element from array


More Examples

Let’s have look over more example and demo here.


Php delete element from array by value

You can delete the array element by value as below –

Php delete element from array by value Example:

'2',"c"=>'1',"d"=>'4',"e"=>'9');
print_r($array);
$delete_value = 4;
if(($key = array_search($delete_value, $array)) !== false) {
    unset($array[$key]);
}
echo "

After Deleting

"; print_r($array); ?>

Try it »

If you run the above example it will produce output something like this-

Php delete element from array by value Example:

Php delete element from array by key

You can delete the array element by key as below –

Php delete element from array by key Example:

'2',"c"=>'1',"d"=>'4',"e"=>'9');
print_r($array);
$key = 'c';
    unset($array[$key]);
echo "

After Deleting

"; print_r($array); ?>

Try it »

If you run the above example it will produce output something like this-

Php delete element from array by value Example:

The requested PHP extension ext-intl * is missing from your system


The requested PHP extension ext-intl * is missing from your system : While installing magento2 via composer this error occurred. This error also appears when installing Cakephp 3. You can fix this problem by following the simple steps as below.


The requested PHP extension ext-intl * is missing from your system

Here is the screen shot of composer command –

The requested PHP extension ext-intl * is missing from your system

Steps to fix the problem –

  • Open php.ini located at – /xampp/php/php.ini
  • Uncomment ;extension=php_intl.dll ie. remove semocolon so it will be – extension=php_intl.dll
  • Restart apache from Xampp control panel.

The above solution will fix our problem.

Php save image from url


Php save image from url : There are many ways to save images from the the given url. file_get_contents and file_put_contents can be used to save images from the given url. For using the file_get_contents allow_url_fopen should be set true. Here in this tutorial we are going to explain how you can save an image from the url.


Php save image from url

Here are the methods you can use to save the image from given url –

Php save image from url : Using file_get_contents

If allow_url_fopen is set to true you can save image from given url as below –

Php save image from url : Using file_get_contents


$url = 'http://example.com/test-image.jpg';
$content = file_get_contents($url);
file_put_contents("/folder/myimage.jpg", $content);

Suppose that your image is on given url like – ‘http://example.com/test-image.jpg’ and you want to download it. First get the content of image using the function file_get_contents($url) where url is image full url. Now to save this image use the function file_put_contents(“/folder/myimage.jpg”, $content); where ‘/folder/myimage.jpg’ is folder name where you want to save the image with the name ‘myimage.jpg’ and $content contains the image content taken from the url.

Php save image from url : Using curl

You can save image from given url using the curl as below –

Php save image from url : Using Curl


$url = 'http://example.com/test-image.jpg';
$ch = curl_init($url);
$fp = fopen('/folder/myimage.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

The above example will save the image in folder with the name ‘myimage.jpg’.

Note : For using curl make sure the curl is enabled on your server.

Php remove first character from string


Php remove first character from string : Sometimes in few cases we need to remove first character from string in php. There are many ways to remove first character from string. Here in this tutorial we are going to explain the simplest method with example and demo.


Php remove first character from string Syntax & Example

You can use substr() function to remove the first character from string as below –

Php remove first character from string : Remove First Letter Using substr


Try it »

If you run the above example it will produce the output something like this –

Php remove first character from string


More Example :

Let us have some more examples –


Remove first character from string : Using ltrim

If you know the first character, you can remove the string as below using the ltrim function-

Php remove first character from string : Using substr


Try it »

Remove first character from string : Using Regular Expression

You can remove first letter using the regular expression also as below –

Php remove first character from string : Using Regular Expression Example


Try it »

If you run the above example it will produce the output something like this –

Remove first character from string : Using Regular Expression

Php Convert object to Associative Array


Php Convert object to Associative Array : We sometimes need to convert the php objects to associative array. An Object is converted to array which contains the keys that are member variables and values are object’s property. Here in this tutorial we are going to explain how you can convert php objects as associative array.


Php Convert object to Associative Array

Let us create a simple php object

Create Php Object : Example

firstName = "John";
$object->lastName = "Dee";

?>

Php objects are accessed in php as below-

Create Php Object : Example

firstName;
echo $object->lastName;

?>

The above example will give you the first name and last name.

Now let us convert the above php object in associative array –

Convert PHP object to Associative Array Example

Php Convert object to Associative Array : Example

firstName = "John";
$object->lastName = "Dee";

$array = (array)$object;
echo $array['firstName']."
"; echo $array['lastName']."
"; ?>

Try it »

$array[‘firstName’] give you the first name and $array[‘lastName’] will give you last name as associative key pair value. If you run the above example it will produce the following output –

Php Convert object to Associative Array

Exception : Some complex object such as private, protected variable are not converted to array properly using this method.