find duplicate values in an array using php

Many times we need to find duplicate values in an array using php. Using php language this is very easy. For checking the duplicate array value array_unique function is very useful.

There too many array and string functions available in php. PHP itself gives very useful methods for finding the duplicate values from array element.

There are multiple ways to find the duplicate strings. you can use loop and strpos method but using following code snippet, it is very easy to find the duplicate values from an array.

find duplicate values in an array using php

find duplicate values in an array using php
find duplicate values in an array using php

Here I am giving you very simple php example:


<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
 $array1 = array("banana","apple","pear","banana");

 if (count(array_unique($array1)) < count($array1))
 echo 'Duplicate entry found in array';
 else
 echo 'No Duplicate values found in array';
 ?>

Output:  Duplicate entry found in array

php check if file exists if not create

Many times we got requirement of check file from specific directory. PHP tutorial for, php check if file exists if not create. Following code will work in all the system like (Mac, Linux and Windows). we given code snippet for checking file exists or not.

php check if file exists if not create
php check if file exists if not create

Using following php code you can check file is present or not in specific directory.


<?php
 $filename = '/var/www/html/test/test.txt';

 if (file_exists($filename)) {
 echo "The file $filename exists";
 } else {
 echo "The file $filename does not exist";
 }

?>

How to check string in string variable using php

PHP tutorial, we will show you how to check string in php variable using php language. This easy very easy using php language. we given code snippet.

Use can use the strpos function for string to search in

<?php
$mystring = 'abc xyz';
$findme&nbsp;&nbsp; = 'abc';
$check_string = strpos($mystring, $findme);


// because the position of 'a' was the 0th (first) character.
if ($check_string === false) {
 echo "The string '$findme' was not found in the string '$mystring'";
} else {
 echo "The string '$findme' was found in the string '$mystring'";
 echo " and exists at position $pos";
}
?>

how to change date format in php

We will show, how to change date format in php code. If you want to change the date format and save into mysql database. You can use our php code or script. You can use following php functions.

how to change date format in php

Let say you are getting date in yyyy-mm-dd format and you want to convert that into dd-mm-yyyy format and save into database. so you can use following function.

$timestamp = strtotime(your date variable);
$new_date = date('d-m-Y', $timestamp);

// or use can following code

$new_date = date('d-m-Y', strtotime(your date variable));

// or use can following code

strftime ($time, '%d %m %Y')
how to change date format in php
how to change date format in php

Solved : fopen and fread not working with url in php

Many times we normally face problems while reading a file using url. Due to wrong PHP configuration we are facing fopen and fread not working with url issue.

fopen and fread not working with url

Using following solution we can easily fix this PHP issue.
For e.g :
$url = ‘http://somesite.com/somefile.txt’;
$fp = fopen($url,”r”);

To allow the fopen to get the contents of the file through url, do the following settings in php.ini file :

allow_url_include = On

By setting above 2 directives to ‘On’, fopen will be able to read the contents from url.

If you are using wamp than we can change this setting by on clicking on wamp icon and than go to PHP and click on setting > and allow_url_fopen option setting.

For linux users, you need to change /etc/php.ini file and do not forget to restart to apache server.

How to execute linux commands using php

Many times we need to run the linux commands using PHP language. We are going to give you code to, execute linux commands using php. This is my favorite question about PHP when I take an interview of new PHP developer.

execute linux commands using php
execute linux commands using php

In this article I will show you how to execute the linux command using PHP language syntax or script.

You can execute linux commands within a php script – all you have to do is put the command line in backticks (”) ,exec() , and shell_exec().

$command = exec('linux command');
echo $command;
echo shell_exec('ll');

How to read xml using php

Many new PHP developer looking for how to easily read the xml file. In this tutorial I will show you how to read the xml file using PHP language. Here I given the sample code for parsing the XML using PHP.

How to read xml using php


This is my xml file format and file name is readxml.xml file


<?xml version="1.0"?>

<!-- our XML-document describes a purchase order -->
<purchase-order>

 <date>2005-10-31</date>
 <number>12345</number>

 <purchased-by>
 <name>My name</name>
 <address>My address</address>
 </purchased-by>

 <!-- a collection element, contains a set of items -->
 <order-items>

 <item>
 <code>687</code>
 <type>CD</type>
 <label>Some music</label>
 </item>

 <item>
 <code>129851</code>
 <type>DVD</type>
 <label>Some video</label>
 </item>

 </order-items>

</purchase-order>

This is one php file called test.php and code as follows


<?php
 //create new document object
 $dom_object = new DOMDocument();
 //load xml file
 $dom_object->load("test.xml");

 $item = $dom_object->getElementsByTagName("item");

 foreach( $item as $value )
 {
 $codes = $value->getElementsByTagName("code");
 $code  = $codes->item(0)->nodeValue;

 $types = $value->getElementsByTagName("type");
 $type  = $types->item(0)->nodeValue;

 $labels = $value->getElementsByTagName("label");
 $label  = $labels->item(0)->nodeValue;

 echo "$code - $type - $label <br>";
 }
?>

When you run the readxml.php file. you will see the following output.

687 – CD – Some music
129851 – DVD – Some video

how to work with multidimensional array in php

If you are programmer then you dont need introduction of array. In every language array is the most important part.

how to work with multidimensional array in php

Using array we can achieve so many things and array is useful for so many times when we do programming.
In this article I am going to share some method about PHP array.

What is PHP array?
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Following is syntax
array(key => value)

Creating PHP array:

<?php
$arr = array("str_arr" => "this is string", 25 => true);

echo $arr["str_arr"]; // print this is string
echo $arr[25];&nbsp;&nbsp;&nbsp; // print ture
?>

How to print direct PHP array.

<?php
$arr=array("a"=>"jimi","b"=>"timi","c"=>"limi");
print_r($arr);
?>

How to print PHP array preformated

<?php
$arr=array("a"=>"jimi","b"=>"timi","c"=>"limi");
print_r("<pre>".$arr);
?>

How to create a two-dimensional PHP array

<?php
$arr = array (
 "fruits"&nbsp; => array("a" => "orange", "b" => "banana", "c" => "apple"),
 "numbers" => array(1, 2, 3, 4, 5, 6),
 "holes"&nbsp;&nbsp; => array("first", 5 => "second", "third")
);
print_r("<pre>".$arr);
?>

How to create an array from MySql table

<?
function cre_array($row) {
 foreach($row as $key => $value) {
 global $$key;
 $$key = $value;
 }
}

//User table has first_name field present.

$sql = mysql_query("SELECT * FROM 'users'");
while ($row = mysql_fetch_assoc($sql)) {
 cre_array($row);
 echo $first_name.'<br>'; //instead of $row['first_name']
}

?>

How to count PHP array count

$fruits = array("a" => "orange", "b" => "banana", "c" => "apple");
echo sizeof($fruits);
echo $arrayLength = count($fruits);

PHP and MySql and Array

<?php
$sql = "SELECT key,value FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
}

while(list($key,$value) = each($myArray)) {
echo "$key : $value";
}
?>

Following image will help you to understand two dimensional array.

How to work with PHP array
How to work with PHP array

how to use soap with php

SOAP is a best technology that can help you in developing web services and amazing applications. Here in this article how you can use the soap with php. You need code in defensive style always and always add the null check and try catch in code. soap is very useful and safe to use for cros domain communication.

how to use soap with php

Many times we heard about soap but we really don’t know about what is soap.

What is soap?

SOAP, the Simple Object Access Protocol, is the powerhouse of web services. It’s a highly adaptable, object-oriented protocol that exists in over 80 implementations on every popular platform, including AppleScript, JavaScript, and Cocoa. It provides a flexible communication layer between applications, regardless of platform and location. As long as they both speak SOAP, a PHP-based web application can ask a C++ database application on another continent to look up the price of a book and have the answer right away. Another Internet Developer article shows how to use SOAP with AppleScript and Perl.

If you want to use the soap then you need to install libxml on your server. In your PHP.ini file

you need to check following setting are available or not if not then put it.

soap.wsdl_cache_enabled 1 PHP_INI_ALL
soap.wsdl_cache_dir /tmp PHP_INI_ALL
soap.wsdl_cache_ttl 86400 PHP_INI_ALL
soap.wsdl_cache 1 PHP_INI_ALL
soap.wsdl_cache_limit 5 PHP_INI_ALL

SOAP is an XML-based web service protocol. The most common external specifications used by a SOAP-based service is WSDL to describe its available services, and that, in turn, usually relies on XML Schema Data (XSD) to describe its data types. In order to “know” SOAP, it would be extremely useful to have some knowledge of WSDL and XSD. This will allow one to figure out how to use the majority of SOAP services.

Here is the basic WSDL Structure



 
 …
 
 
 …
 
 
 …
 
 
 …
 
 

Filled example:

 
 
 
 
 
 
 
 
 
 
 
 
 

Soap is nothing but xml format web service to talk with cross domain servers. There are other ways also to create web services. XML-RPC is good one and that is not too much hard to learn also. Use can use following code in your PHP file.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]


// create a soap object
 $Soap_Object = new SoapClient("http://anywebsite.com/any.wsdl", array('trace' => true));

 // setting params to authenticate to web service
 $params_Authenticate = array('username' => 'username', 'password' => 'password');

 // authenticate to web service
 $do_login=&nbsp; $Soap_Object->login($params_Authenticate);

 // take sesstion id from anywebsite.com
 $your_session_id= $do_login->loginReturn->sessionId;

 // Set the query parameters.

 $soap_query = 'select book_id from Time where book = 52435455';
 paramsQry = array('queryString' => $soap_query);

 // Make a soap call.
 $objResponse = $Soap_Object->query(paramsQry);

 header('Content-Type: text/xml; ');
 print($Soap_Object->__getLastRequest());

[/viral-lock]
Soap is nothing but a web service with xml so use PHP functions to create the Soap service. Following graph will help you to understand more and in detail about SOAP.

how to use soap with php
how to use soap with php

how to send email using php – Using PHPmailer

Many PHP programmers facing this issue and hitting same wall. Here in article we shown, how to send email using php. we given code sample with phpmailer.

how to send email using php

how to send email using php - PHP tutorial
how to send email using php – PHP tutorial

PHP itself provides the mail() function to send emails. In this article I will show you the options of sending email using PHP language.

If you are using linux or windows hosting service for running the PHP application. PHP mail() function uses the sendmail mail server to send emails.
If sendmail mail server is configured and running on linux server then only mail() function is able to send the emails.

First we will talk about mail() function. You can pass the following parameters to mail function.

mail(to,subject,message,headers,parameters)

Sample code as follows:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$to = "support@purabtech.in/files/, wordpressapi@gmail.com";
$subject = "Test mail";
$message = "This is a simple email message.
 this is seond line.";
$from = "some@example.com, wordpressapi@gmail.com";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";

$headers .= 'Cc: test@test1.com' . "\r\n";
$headers .= 'Bcc: test2@test1.com' . "\r\n";

mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

If sendmail mail server is not configured on your server, mostly with windows server then there is issue.
But you can send email using anothere PHP programm called PHPmailer.
you can download the PHPmailer from following URL:

http://sourceforge.net/projects/phpmailer/

Download first PHPmailer and extract the phpmailer folder and in that folder you will find the following three files.
1. class.phpmailer.php
2. class.pop3.php
3. class.smtp.php

copy and paste the following files to your PHP application. and use the following code in case of use POP email

<?php
function sendmail_wordpressapi($to,$from,$subject,$body,$id){
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "purabtech.in/files/"; // SMTP server
$mail->From       = $from;
$mail->FromName   = $from;
$mail->Subject    = $subject;
$mail->Body       = $body;
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress($to, $to);

if(!$mail->Send()) {
$mail             = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "WORDPRESSAPI@gmail.com";  // GMAIL username
$mail->Password   = "YOURPASSWORD";            // GMAIL password
$mail->AddReplyTo("smtp.production@gmail.com","wordpressapi");
$mail->From       = "support@purabtech.in/files/";
$mail->FromName   = "SMTP EMAIL-wordpressapi";
$mail->Subject    = "email server is having some issue";
$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("support@purabtech.in/files/", "support-wordpressapi");
$mail->IsHTML(true); // send as HTML
$mail->Send();

} else {
 echo "Message sent!";
}
}
?>

I created above function in this function I am using SMTP mail function for sending email but in case mail is not going using SMTP details.
you can also send emails using your gmail account details. you can edit or use this function as your requirement.

You can pass following parameters to sendmail_wordpressapi function:

sendmail_wordpressapi($to,$from,$subject,$body,$id);

You can send email to multiple email addresses. I am sure using above code sending emails through PHP is very easy. If you have any quries..write to me.