Monday, August 31, 2015

Replace Multiple String with PHP


Replace Multiple String In PHP
If you are working with PHP, then you would meet some problem with how to replace multiple string with a line of code. So there I will show you how to replace multiple string to make your url to be friendly.
In this case you have to know a PHP function which is call "str_replace", the below code will explain you how it work.

function replace_multiple_string($search=array(),$replace="",$string="")
{
  if(count($search) < 1)
  {
    return false;
  }
  else
  {
    return str_replace($search, $replace, $string);
  }
}

Explanation:
In the function above you would see the str_replace function which I've use to remove and replace the underscore instead of $replaces array.

 - str_replace : Replace all occurrences of the search string with the replacement string.

Usage : 

$string= "You are on_tip(and)php+website";
$search= array("_", "(", "+", ")"," ","+");

var_dump(replace_multiple_string($search,"-",$string));

Demo :

string(29) "You-are-on-tip-php-website" 

Saturday, August 29, 2015

Get Mac Address With PHP


get mac address with php

This post is to show you about Getting Mac Address or PC Address using PHP. This script will help you to display the Address of your computer. Based on this function I hope you will like it. Please take a look at live demo and to see your computer address.

Function (index.php)

function get_mac_address()
{
  ob_start(); 
  system('ipconfig /all'); 
  $mycom=ob_get_contents();
  ob_clean(); 
  $findme = "Physical";
  $pmac = strpos($mycom, $findme);

  return substr($mycom,($pmac+36),17);
}


Explanation

- ob_start() : Turn on output buffering
- system('ipconfig /all') : Get the ipconfig details using system commond.
- ob_get_contents() : Return the contents of the output buffer
- ob_clean() : clear or Erase output buffering.

Call the function (index.php)/ Demo

<?php get_mac_address(); ?>


I hope you will share if you think it is important.