Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, 11 August 2017

PHP script to convert string into 1337 characters

A very basic PHP code, for leet users, that will convert your string into leet characters.



[php]
<?php
/**
* PHP code to convert your string to leet characters.
*/
$input =  strtolower($argv[1]);
$output = "";
$count = strlen($input);
for ($i=0;$i<$count;$i++) {
  switch ($input[$i]) {
    case "a" : $output .= "4";
               break;
    case "b" : $output .= "8";
               break;
    case "c" : $output .= "(";
               break;
    case "d" : $output .= "|)";
               break;
    case "e" : $output .= "3";
               break;
    case "f" : $output .= "|\"";
               break;
    case "g" : $output .= "6";
               break;
    case "h" : $output .= "|-|";
               break;
    case "i" : $output .= "1";
               break;
    case "j" : $output .= "_/";
               break;
    case "k" : $output .= "|<";
               break;
    case "l" : $output .= "|_";
               break;
    case "m" : $output .= "|\\/|";
               break;
    case "n" : $output .= "|\\|";
               break;
    case "o" : $output .= "0";
               break;
    case "p" : $output .= "|*";
               break;
    case "q" : $output .= "0_";
               break;
    case "r" : $output .= "12";
               break;
    case "s" : $output .= "5";
               break;
    case "t" : $output .= "7";
               break;
    case "u" : $output .= "|_|";
               break;
    case "v" : $output .= "\\/";
               break;
    case "w" : $output .= "\\/\\/";
               break;
    case "x" : $output .= "><";
               break;
    case "y" : $output .= "`/";
               break;
    case "z" : $output .= ">_";
               break;
    case " " : $output .= " ";
               break;
    default : $output .= $input[$i];
              break;

  }
}
echo $output;
?>
[/php]

Compare two files for duplicate entries using bash and php

Hi Friends,

Need to compare two files and searching for different entries in them. Here is the solution.

Create a new file named cmp.sh and put it in a folder.



[code]
#!/bin/bash
uniq temp1 > sort_temp1
sort sort_temp1 > sort1.txt
uniq temp2 > sort_temp2
sort sort_temp2 >> sort1.txt
sort sort1.txt > sort_dup.txt
cat sort_dup.txt | uniq > sort.txt
echo "Elements present in file temp1 not in file temp2"
php index.php 1
echo "Elements present in file temp1 not in file temp2"
php index.php 2
[code]
Save it.

Now, we need to create a php file named index.php in same folder.
[php]
<?php
$handle = @fopen("sort.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);

//======================================
if($argv[1] == 1)
{
$handle1 = @fopen("sort_temp1", "r");
if ($handle1) {
$test = 1;
while (!feof($handle1)) {
$buffer1 = fgets($handle1, 4096);
if ($buffer==$buffer1)
$test = 0;
}
if ($test == 1)
echo $buffer;
}
fclose($handle1);
}

if($argv[1] == 2)
{
$handle2 = @fopen("sort_temp2", "r");
if ($handle2) {
$test = 1;
while (!feof($handle2)) {
$buffer2 = fgets($handle2, 4096);
if ($buffer==$buffer2)
$test = 0;
}
if ($test == 1)
echo $buffer;
}
fclose($handle2);
}
//========================================

    }
    fclose($handle);
}
?>
[/php]
Save the file in same folder. Add your both files need to compare, with the name temp1 and temp2.

Run the cmd.sh script and it will give you the desired result.

Hope it helps.

count twitter followers using PHP

Hi Members,

If you need to find the number of followers of particular username without logging in to the twitter, then this script will be helpful for you.



Run the script in cli mode and it will give you twitter count.

How to run:
[code]
$ php twitter_follower.php
<follower count>
[/code]

Save the below code as file named twitter_follower.php and  run as instructed above :

[Code]
<?php
$username = "coding-scripting";
$url = "https://twitter.com/$username";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
$pattern = "/followers_count&quot;:(\d+),&quot;/";
if(preg_match($pattern, $data, $count)) {
$followers = $count[1];
print $followers;
}
?>
[Code]

In case of any issue, let me know in comments.

Thanks

Saturday, 9 April 2016

Composer | Windows | PHP Error : "You must enable the openssl extension to download files via https"

PHP - Composer - Windows


Okay, you are trying to install composer, and encountered the above error. This error occurred, if your php_openssl.dll is missing or commented in your php.ini.




Steps to resolve:

1. Download php.zip from here (64 bit version). (Other version can be found here: PHP for Windows)

2. Unzip it and copy all the contents inside of uncompressed folder in to the C:\Program Files\php directory. (Create php directory)

3. Add C:\Program Files\php in your environment PATH variable.

4. Download composer and try to install it.

5. If you get the above error, copy the php.ini-development file present in C:\Program Files\php, and rename it to php.ini file.

6. Open the php.ini file.

7.  Find the ";extension=php_openssl.dll " then change it to "ext/php_openssl.dll" (removed semi-colon and added ext)

and continue with the installation of composer. It will install successfully.

Tuesday, 1 November 2011

Magic Square (Code PHP)

Hello Friends, in my previous post i discussed about the logic for Magic Square. In this post i am posting the code for Magic Square. Save the code in a file named code.php and upload it on your server and see the result. It will definitely work ;) Good luck

Code :-


@author - Aksshay
<html>
<head>
<title>
Magic Square
</title>
</head>
<body>
<!-- Take input from the user and
create the magic square of that dimesion --!>

<form action="code.php" method="GET">
<br />
Enter the dimension for magic square (2-9) :-
<br />
<input type="text" value="3" name="dime" />
<br />
<input type="submit" value="GO" />
</form>
</body>
</html>

<?php

// Magic square
// @ Author : aksshay


if(isset($_GET['dime']))
{
$dime = $_GET['dime'];
$number_of_elements = $dime * $dime;
$values = 1;

for ($row = 0; $row < $dime ; $row++)
    for ($column = 0; $column < $dime; $column++)
        $array_a[$row][$column] = $values++;
       
$middle = $dime/2;
$middle = ceil($middle) - 1;

for ($column = 0; $column<$dime; $column++)
    $array_b[$middle][$column] = $column+1;
   
$temp_b = $middle-1;

while($temp_b >= 0)
{
   
    for ($column = 0; $column < $dime; $column++)
    {
       
        if ((($array_b[$temp_b+1][$column])-1) != 0)
             $array_b[$temp_b][$column] = $array_b[$temp_b+1][$column]-1;
        else
             $array_b[$temp_b][$column] = $dime;   
                   
        }
       
    $temp_b--;
}
   
$temp_b = $middle+1;   
while($temp_b < $dime)
{
   
    for ($column = 0; $column < $dime;$column++)
    {
        if ((($array_b[$temp_b-1][$column])+1) <= $dime)
        $array_b[$temp_b][$column] = $array_b[$temp_b-1][$column]+1;
        else
        {
            $array_b[$temp_b][$column] = $array_b[$temp_b-1][$column]+ 1 - $dime;
                    }
       
        }
       
    $temp_b++;
}

$swape = $dime-1;
for ($row = 0; $row < $dime ; $row++)
{
    for ($column = 0; $column < $dime; $column++)
    {
        $array_c[$row][$column] = $array_b[$swape][$column];
}
$swape--;
}

for ($row = 0; $row < $dime ; $row++)
    for ($column = 0; $column < $dime; $column++)
    {   $row_element = $array_b[$row][$column]-1;
        $column_element = $array_c[$row][$column]-1;
        $magic_square[$row][$column] = $array_a[$row_element][$column_element];
     }
  
?>

<table align="center" width="500" height="100" cellpadding="2" cellspacing="2" bordercolorlight="#159C18">
<?
for ($row = 0; $row < $dime ; $row++)
{
    echo "<tr>";
    for ($column = 0; $column < $dime; $column++)
        echo  "<td>".$magic_square[$row][$column]."\t"."</td>";
    echo "</tr>";
        }
       
        ?>
        <br />
        </table>
        <?php
       
    
}

else
{
echo "Dimensions aren't set";
}
?>

Thanks.

Sunday, 30 October 2011

Magic Square

My next post is about Magic Square :-

What is Magic Square?

The magic square is matrix of n*n elements in which the addition of any row elements, column elements or diagonal elements will produce the same result.


There are many ways to generate the Magic Square, and the logic i used is explained below :-

There are 3 arrays.

1st array contains the number written in array, for example if we have to generate the 3*3 square , then array 1 contain these elements :-

1     2     3
4     5     6
7     8     9

Array 2nd is written in such a fashion that the middle column occupy the elements in order and its adjacent rows are incremented/decremented order.

3     1     2
1     2     3
2     3     1

Array 3rd is exactly the image of array 2nd, so the array 3rd is

2     1     3
3     2     1
1     3     2

Now with the help of these 3 arrays, we are able to create our magic square.

Magic Square [row][column] = Array 1 [array 2 [row][column]][array 2 [row][column]

 Magic Square [1][1] = Array1[3][2]

because, array2 [1][1] = 3, and array3 [1][1] = 2

So, in this manner we are able to calculate all the results. :)

Thats it. The code will be in my next post.

Thanks.

Saturday, 8 October 2011

The Starting of Journey

Well, i am starting this blog from the first entry i created at IVAN's My friend blog related to Image WaterMarking using PHP to protect your images from others.

So here is my first code

example below to make image watermarking using php:


<?php
//// By aksshay sharma - to create watermark images (runtime) using php
// Telling browser that the content is of image type
header('content-type: image/jpeg');
// Setting our watermark image (company logo)
$watermark_image = imagecreatefrompng('watermark_logo.png');
// Calculating Dimension of our watermark
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$your_image = imagecreatetruecolor($watermark_width, $watermark_height);
//Set the image on which watermark is to be made
$your_image = imagecreatefromjpeg("my_image.jpg");
$size = getimagesize("my_image.jpg");
$final_x = $size[0] - $watermark_width - 5;
$final_y = $size[1] - $watermark_height - 5;
imagecopymerge($your_image, $watermark_image, $final_x, $final_y, 0, 0, $watermark_width, $watermark_height, 100);
// Generating image having watermark
imagejpeg($your_image);
// Destroying temporary images
imagedestroy($your_image);
imagedestroy($watermark_image);
?>

Example for watermark image with text using PHP:

<?php
//Using imagecopymerge() to create a translucent watermark
// Load the image on which watermark is to be applied
$original_image = imagecreatefromjpeg('pic.jpeg');


// First we create our watermark image manually from GD
$watermark = imagecreatetruecolor(100, 70);
$original_image = imagecreatefromjpeg('pic.jpeg');
//Set the hex color code for your watermark and dimension
imagestring($watermark, 5, 20, 20, 'Aksshay', 0xFFFFF);
imagestring($watermark, 3, 20, 40, 'Sharma', 0xFFFFF);


// Set the margins for the watermark and get the height/width of the watermark image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);


// Merge the watermark onto our photo with an opacity (transparency) of 50%
imagecopymerge($original_image, $watermark, imagesx($original_image) - $sx - $marge_right, imagesy($original_image) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark), 50);


// Save the image to file and free memory
imagepng($original_image, 'watermark_image.png');
imagedestroy($original_image);
?>


The first code will watermark an image with another image, so it’s like to stamp the other image with your digital sign. And the second example, it will stamp your image with your text. Basically the text will converted to image then stamp to the original image.

To use the code don’t forget to enable php gd library in your php.ini:

extension=php_gd2.dll

Note: Your watermark image is of 8-bit PNG. To make your image 8-bit PNG you can use imagemagik.

Copy the complete code from here -

Complete Code