miércoles, 6 de abril de 2016

php couting leters and numbers in an array

<?php



$str = '+0123qq';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

echo "<br>";


$num=0;
$letters=0;

foreach($chars as $elements){

if (is_numeric($elements)) {
$num+=1;

}



else{
$letters+=1;

}



}

echo " array got $num  numbers";
echo "<br>";
echo " array got $letters  letters";

?>

preg_split (PHP 4, PHP 5, PHP 7) preg_split — Split string by a regular expression

Examples ¶

Example #1 preg_split() example : Get the parts of a search string
<?php// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");print_r($keywords);?>
The above example will output:
Array
(
    [0] => hypertext
    [1] => language
    [2] => programming
)
Example #2 Splitting a string into component characters
<?php
$str 
= 'string';$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);print_r($chars);?>
The above example will output:
Array
(
    [0] => s
    [1] => t
    [2] => r
    [3] => i
    [4] => n
    [5] => g
)

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.

domingo, 28 de febrero de 2016

PHP curl

If you’re a Linux user then you’ve probably used cURL. It’s a powerful tool used for everything from sending email to downloading the latest My Little Pony subtitles. In this article I’ll explain how to use the cURL extension in PHP. The extension offers us the functionality as the console utility in the comfortable world of PHP. I’ll discuss sending GET and POST requests, handling login cookies, and FTP functionality.
Before we begin, make sure you have the extension (and the libcURL library) installed. It’s not installed by default. In most cases it can be installed using your system’s package manager, but barring that you can find instructions in the PHP manual.

How Does it Work?

All cURL requests follow the same basic pattern:
  1. First we initialize the cURL resource (often abbreviated as ch for “cURL handle”) by calling thecurl_init() function.
  2. Next we set various options, such as the URL, request method, payload data, etc. Options can be set individually with curl_setopt(), or we can pass an array of options to curl_setopt_array().
  3. Then we execute the request by calling curl_exec().
  4. Finally, we free the resource to clear out memory.