Friday, April 21, 2017

Best way to store HTML code in MYSQL and echo from mysql PHP

Best way to store HTML code in MYSQL database and echo/display from mysql database to browser using PHP.

htmlentities(HTML, ENT_QUOTES, "UTF-8") function — Convert all applicable characters to HTML entities.

This function Returns the encoded string. If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

$str = "A 'quote' is <b>bold</b>";

echo $str = htmlentities($str, ENT_QUOTES);// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;

If you want to decode instead (the reverse) you can use html_entity_decode().

echo html_entity_decode($str);
// A 'quote' is <b>bold</b>

Tuesday, April 18, 2017

Website Document Root outside public_html for addon/sub domains

When adding an Addon OR Sub domain, the default DocumentRoot on cPanel servers is /home/user/public_html/addon.com.

What if you want to set the DocumentRoot outside public_html ? You can when adding Addon OR sub domain. But, the system WHM won’t allow it. To enable this feature, you need to contact your web hosting provider. Not all web hosts provide this feature. If you have a VPS hosting OR dedicated server, login to your server’s WHM as root. Access option Server Configuration >> Tweak Settings. Look for Restrict document roots to public_html 


This setting prevents the creation of addon domains and subdomains outside of a cPanel user's primary domain's document root (the public_html directory within the user's /home directory).

 For example, if you enable this setting and then create the example.com addon domain, the system creates the /home/username/public_html/example.com directory rather than the /home/username/example.com directory.

This setting defaults to On. Turn it off. Now you can make subdomain's document root outside of public_html folder.

Tuesday, April 11, 2017

Display numbers with ordinal suffix/position in PHP

I want to display numbers/any position like merit position as follows
  • 1 as 1st,
  • 2 as 2nd,
  • 3 as 3rd
  • 21 as 21st
  • ...,
  • 150 as 150th.
As a function:
function ordinal_position($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}
//Example Usage
echo ordinal_position(121);
// show 121st


Total Pageviews