Skip to main content

Posts

Showing posts from February, 2011

php split array and join 2 arrays

Lets take the example of a polygon, with its vertices forming an array. <?php $x = $y = array(); // An array representing a polygon $polygon = array("10 0", "20 5", "15 15", "22 15"); print_r($polygon); echo '<br>'; // Break this into 2 arrays, with x-coordinates and y-coordinates forming individual arrays foreach ($polygon as $coor) {     list($x[], $y[]) = explode(' ', $coor); } print_r($x); echo '<br>'; print_r($y); echo '<br>'; // Now combine these arrays to get back the polygon array for ($i = 0; $i<count($x); $i++) {     $poly[] = $x[$i] .' ' . $y[$i]; } print_r($poly); ?> Output: Array ( [0] => 10 0 [1] => 20 5 [2] => 15 15 [3] => 22 15 ) Array ( [0] => 10 [1] => 20 [2] => 15 [3] => 22 ) Array ( [0] => 0 [1] => 5 [2] => 15 [3] => 15 ) Array ( [0] =>

php preg_match examples

<?php $subject = "abcdef ghij"; echo 'String: '.$subject.'<br><br>'; $key = 'def'; $non_match = ''; if(preg_match("/^(.*)$key(.*)$/",$subject,$m)) {         $non_match= $m[1].$m[2]; } echo 'Text not matching {def}: '.$non_match.'<br>'; $subject = explode( " ", $subject ); foreach( $subject as $k => $v ) if( preg_match( "/abcdef/", $v )) {   unset( $subject[$k] ); } $subject = implode( " ", $subject ); echo 'Text not matching {abcdef}: '.$subject.'<br><br>'; $text = 'POLYGON((0 0,20 0,20 20,0 20,0 0))'; echo 'String: '.$text.'<br><br>'; preg_match('#\((.*?)\)#', $text, $match); print 'Text in parenthesis: '.substr($match[1],1); ?> Output: String: abcdef ghij Text not matching {def}: abc ghij Text not matching {abcdef}: ghij String: POLYGON((0 0,20 0,20

php get content between tags

This involves parsing the dom document. <?php function getTextBetweenTags($tag, $html) {     $dom = new domDocument;     @$dom->loadHTML($html);         $dom->preserveWhiteSpace = false;     $content = $dom->getElementsByTagname($tag);     $out = array();         foreach ($content as $item)     {         $out[] = $item->nodeValue;     }     return $out; } $xhtml = '<tag>abc def</tag><tag>123 456</tag>'; $content2 = getTextBetweenTags('tag', $xhtml); foreach( $content2 as $item ) {     echo $item.'<br />'; } ?> Output: abc def 123 456

Paypal Integration Sample code

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <!-- Identify your business so that you can collect the payments. --> <input type="hidden" name="business" value="dskanth.99@gmail.com"> <!-- Specify a Buy Now button. --> <input type="hidden" name="cmd" value="_xclick"> <!-- Specify details about the item that buyers will purchase. --> <input type="hidden" name="item_name" value="A Sample Product"> <input type="hidden" name="amount" value="3000"> <input type="hidden" name="currency_code" value="USD"> <!-- Display the payment button. --> <input type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay on