Use the SUBSTR command: $strWithoutLastChar = substr(“abcdef”, 0, -1); This will return “abcde”
How do I remove all characters from within curly brackets or square brackets using PHP
Use preg_replace and regular expressions. The examples below remove everything in a string that is within curly braces or square brackets. echo preg_replace(” (\[.*?\])”,”,”King Kong [2005]”); echo preg_replace(” (\(.*?\))”,”,”King Kong (Amazing Box Set)”);
How to get the Filename out of a directory name using PHP
Use strrpos – same as strpos but in reverse. Find the last occurence of ‘/’ then move forward one position and then substr from that position onwards to the end of the string. <?php echo substr($url,strrpos($url,’/’)+1); ?>
How to set the browser timeout in PHP
If your script is too complex to finish within the standard 30 seconds you can set a new value with the function: set_time_limit(900); this sets the timeout too 900 seconds / 15 minutes.
How to find out whether a number is odd or even using PHP
$i = 10; if ( $i&1 ) { echo “$i is odd”; } else { echo “$i is even”; }
How to check whether a string contains HTML using PHP
There are two ways you can do it, one is using a regular expression the other is comparing strings. First the regular expression approach: if (preg_match(“/([\<])([^\>]{1,})*([\>])/i”, $string)) { echo “string contains html”; } And here is the other approach: if(strlen($string)
How to generate a random number in PHP
To generate a random number between 0 and 100 do this: srand((double)microtime()*1000000); echo rand(0,100);
How to insert javascript in php code
Just use PHP echo commands. <? echo “<script language=\”JavaScript\”>\n” ; echo “alert(” javascript from php “);\n” ; echo “</script>” ; ?>
How to get a user’s IP address using PHP
<? $domain = GetHostByName ( $REMOTE_ADDR ); ?>