Tuesday, April 26, 2011

Validating the constant name (Recurcivly)

It will recursively check that given text is valid constant name or not.

Pattern Rules:
1. Only A to Z , underscore (_) are allowed.

2. Repeating underscores (_) at once are now allowed.

3. Up to 45 chars allowed.

<?php  


/*
    Validating the constant name (Recurcivly)
    -----------------------------------------
    (Logic/Pattern) Written by Junaid Atari

    + Examples:
      - ACCESS_IS_ALLOWED
      - DIR_SEPERATOR
      - HOLDER
      - N_A_M_E_I_S_N_O_N_E
*/

//# Constant name to validate.

$name 'ACTION_STATUS';

echo 
preg_match ('/^([A-Z]{1}([_]?)){2,43}[A-Z]$/'$name)
        ? 
'Constant name is Valid'
        
'
Constant name is not valid';

Validating the name (Recurcivly)

It will recursively check that given text is valid name or not.

Pattern Rules:
1. Only A to Z, a to z , space and - (dash) are allowed.

2. Repeated spaces, dashes (-) at once are now allowed.

<?php
/*
    Validating the name (Recurcivly)
    -----------------------------------------
    (Logic/Pattern) Written by Junaid Atari
   
    + Examples:
      - Muhammad
      - Junaid Atari
      - Sag-e-Attar       
*/

//# Name to validate.

$name 'Ali Attar';

echo 
preg_match ('/^([a-zA-Z]+)(?:[a-zA-Z]+[\s-]?)*$/'$name)
        ? 
'Name is Valid'
        
'Name is not valid';

Validating the URL

Here I have wrote the recursive PCRE pattern for URL, hope you like it.

Recursive URL pattern features:
-------------------------------------------------------
1. Protocols https|http|ftp|ftps
2. WWW optional or only one.
3. Subdomain (unlimited) (Recursively match the subdomains) & (repeatedly - . at
    same time are not allowed).
4. domain only one (repeatedly - . at same time are not allowed).
5. extention only one mimimum 2, max five (repeatedly . at same time are not allowed).
6. End forward slash (/) is Optional.

<?php

/**
 * is_valid_url ()
 * Validate that given text is valid URL or not.
 *
 * @author   Junaid Atari <mj.atari@gmail.com>
 * @version  1.0
 * @param    string  $str    Text to validate.
 * @return   bool    TRUE on valid | FALSE on invalid.
 */

function is_valid_url ($str)
{
    
//** Return if string not given or empty.
    
if (!is_string ($str)
        || 
trim ($str) == '')
            return 
false;
   
    
//** Recursive url pattern written by Junaid Atari.
    
return preg_match '/^((http|ftp)(s?):\/\/)(www\.)?'.
                        
'([a-z0-9]{1}([\-\.])?)*'.
                        
'([a-z0-9]{1}([\-])?){1}'.
                        
'(\.[a-z]{2,4})\/?$/i'$str ) == 0
        
false
        
true;
}
/*
 +=======================================
 | Example
 +=======================================
*/
$urls = array (
    
'http://www.yahoo.com/',
    
'http://code.google.com',
    
'https://login.live.com/',
    
'http://my.name.is.domain.com.pk/',
    
'https://www.name.is.2k-pcre.co.uk/',
);

foreach (
$urls as $url)
    echo 
$url ." (" . (is_valid_url ($url)
                        ? 
'Valid'
                        
'Not valid') . ")<br />";
/*
 +=======================================
 | Output
 +=======================================
*/

/*
 http://www.yahoo.com/ (Valid)
 http://code.google.com (Valid)
 https://login.live.com/ (Valid)
 http://my.name.is.domain.com.pk/ (Valid)
 https://www.name.is.2k-pcre.co.uk/ (Valid)
*/

Recursively remove the empty html tags

Hello Guys,

Here i have wrote the new function and REGEX pattern to remove the nested empty tags (nested) from string. Watch yourself below.

<?php

/**
 * remove_empty_tags_recursive ()
 * Remove the nested HTML empty tags from the string.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version    1.0
 * @param    string    $str    String to remove tags.
 * @param    string    $repto    Replace empty string with.
 * @return    string    Cleaned string.
 */

function 
remove_empty_tags_recursive ($str$repto NULL)
{
    
//** Return if string not given or empty.
    
if (!is_string ($str)
        || 
trim ($str) == '')
            return 
$str;

    
//** Recursive empty HTML tags.
    
return preg_replace (

        
//** Pattern written by Junaid Atari.
        
'/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU',

        
//** Replace with nothing if string empty.
        
!is_string ($repto) ? '' $repto,

        
//** Source string
        
$str
    
);
}
/*
+=====================================
| EXAMPLE
+=====================================
*/
$str=<<<EOF

<p></p>
<div id="paralax">
    <div><p></p>Hello User,<strong></strong></div>
    <div id="contents">Welcome to our domain.</div><p><p><p></p></p></p>
</div>

EOF;

echo 
remove_empty_tags_recursive ($str);
/*
+=====================================
| OUTPUT:
+=====================================
*/

/*<div id="paralax">
    <div>Hello User,</div>
    <div id="contents">Welcome to our domain.</div>
</div>*/


Hope you like it.