Posts Tagged ‘php’
Replace all spaces in HTML with except in HTML tags using PHP
If you would like to replace all the spaces in an HTML string with so that you can render it out to the browser but you also want to retain current spaces in HTML tags you can use the below method: $inputString = ‘<span class="color:red">1</span> 2 3′; $outputString = preg_replace_callback("#>[^<]+<#","relSpaces",$inputString); function relSpaces($match){return str_replace(" "," ",$match[0]);} This will replace: “<span>1</span> 2 3″ ..to look like this: “<span>1</span> 2 3″
Using PHP to validate an IP address
You can use the PHP code below to check if an IP address is valid or not. $ip = ""; //enter a valid or invalid ip address here if(filter_var($ip, FILTER_VALIDATE_IP)) { // The IP Address is valid } else { // The IP Address is not valid } Wasn’t that easy!?!
Stop That Referrer in PHP!
Today I will show you a method to stop the traffic that a referrer site is sending your way, you may wonder why you would want to ignore traffic, after all, isn’t inbound traffic to your site fantastic no matter what? The simple answer is “NO!” Let me explain it in a real life situation so that you can better understand where to use this. Understanding where to use it…
CSS background image data: base64?
What the heck is all that Gobbledygook in the CSS? So you have noticed that bit of css that says something like this in the CSS source-code and you have no idea what it could be? url(“data:image/gif;base64,R0lGODlfMAMEAIABAKysrObm5iH5BAEAAAEALAAAAAAwAwQAAAJOhI+py30Bo5y02ouz3rz7D4biSJbmiabqyrbuC8fyTKPOjedHzff+DwwKh8Si8YhMKku6pvOxjEqn1Kr1is1qt7ynV8cNi8fksvmMTiMLAD4=”) no-repeat scroll 50% 0 transparent It is a technique called Data URLs and using PHP – or your favourite server-side script – you can generate these nifty little things. An example of how…
PHP's typeof – gettype()
Quite often you may need the ability to check what type of data type a variable is. In most programming languages this is possible to do and is usually called typeof(). PHP too has a way to check this, it is called gettype() and it’s so simple to use. All you have to do is echo out a call to gettype() with your variable as the first argument. Like so:…
Actionscript 2 & PHP Data Transfer
In the below example we will use Actionscript 2 to call a remote PHP file that will return data to it. It is a simple way of transferring data between the server and client. Actionscript 2 Code: callRemoteFile = function(ourVariable:String) { var result_lv:LoadVars = new LoadVars(); result_lv.onLoad = function(success:Boolean) { if (success) { …
Unique Random Numbers in PHP
If you would like to show random numbers using PHP you can do this: <?php $min = 1; $max = 100; $total = 100; $arrItems = array(); while ( count($arrItems) < $total ) { $item = mt_rand($min,$max); …
HTML mail() Sending as Plain Text
$to = "email@address.com"; $subject = "SUBJECT"; $message = "<b>MESSAGE</b>"; $headers = ‘MIME-Version: 1.0′ . "\r\n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "\r\n"; $headers .= ‘From: Mailer ‘ . "\r\n"; mail($to, $subject, $message, $headers); The above code does not always send emails in HTML as it should, it turns out there is a PHP Bug (http://bugs.php.net/15841) that comes into play with QMAIL and carriage returns. If this is the case you…
A PHP Mail Class
Below is a Mail class I created in PHP, it requires 4 arguments on initiation of the class via the constructor and uses the method ->send() to send the created mail once complete. class Mail { var $to; var $subject; var $content; var $headers; function Mail($to, $subject, $content, $from){ …
unexpected T_IF
Getting “Parse error: syntax error, unexpected T_IF” ? Check if the preceeding line ends with a ;
MySQL Main Query Types used in PHP to select, insert, update and delete data
MySQL Main Query Types SELECT SELECT * FROM tablename INSERT INSERT INTO tablename (col1, col2, col3,..) VALUES (val1, val2, val3,…) UPDATE UPDATE tablename SET `col1`=`val1`, `col2`=`val2`, `col3`=`val3`,… DELETE DELETE FROM tablename WHERE `col4`=`val6` Note You will use a lot of WHERE clauses as well along with the above. e.g. SELECT * FROM tablename WHERE `id`=’15′ In PHP Calling to MySQL is really easy in PHP, just use the mysql_query() function….
Connect to mysql database from php
If you need to connect to a mysql database from php you can do it like this: <?php> $DBH = ‘localhost’; $DBU = ‘root’; $DBPWD = ‘password’; $DBN = ‘petstore’; $conn = mysql_connect($DBH, $DBU, $DBPWD) or die ("Error: Could not connect to database."); mysql_select_db($dbname); ?> This allows you to make a connection to the mysql database and gets it ready for you to make…