martes, 29 de marzo de 2016

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(Note that the double quoted string syntax is perfectly correct)
Editor's note: using this code has security implications. The client can set HTTP_HOST to any arbitrary value it wants.
http://stackoverflow.com/questions/6768793/get-the-full-url-in-php

sábado, 19 de marzo de 2016

Using indexed arrays with preg_replace()

<?php

$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();


$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';


$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';



echo preg_replace($patterns, $replacements, $string);


The bear black slow jumped over the lazy dog.

replacing all spaces by * character

<?php



$text="  hola amigo         ";
 $text = preg_replace('/\s+/','*',$text);

echo  $text;


ouput
*hola*amigo*

martes, 1 de marzo de 2016

How to Convert Data from MySQL to JSON using PHP

On 1/12/2015
In PHP, Converting Data from MySQL to JSON Format is one of the prominent tasks in Web Development. JSON has gained popularity over the years and is preferred over xml as data exchange format between web applications.
Using json format has its own advantages like being light weight, ability to store complex data structures in plain text and very human readable. Earlier we have discussed about converting json to mysql datahere. Now let us see how to convert mysql result set to json in php.