Add http if not exists with url




function addhttp($url) {

 if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
 }
 return $url;
}

$url = 'http://www.google.com';

echo addhttp($url);


Advertisements

Scan images inside folder with PHP


</pre>
<?php
 $image_path = 'images/';
 $tch = '';
 if(is_dir($image_path))
 {
 $siteurl = 'http://example.com';
 if (substr($siteurl , -1) !== '/')
 {
 $siteurl = $siteurl ."/";
 }
 $tch_dirhandle = opendir($image_path);
 while ($tch_file = readdir($tch_dirhandle))
 {
 $tch_file_nocaps = $tch_file;
 $tch_file = strtoupper($tch_file);
 if(!is_dir($tch_file) && ((strpos($tch_file, '.JPG')>0) or (strpos($tch_file, '.GIF')>0) or (strpos($tch_file, '.PNG')>0) or (strpos($tch_file, '.JPEG')>0)))
 {
 echo "<a href='".$siteurl . $image_path . $tch_file_nocaps."' rel='lightbox'>";
 echo "<img class='siteimg' src='".$siteurl . $image_path . $tch_file_nocaps."' /></a>";
 }
 }
 }
 ?>

displaying category image in woocommerce


</pre>
/* displaying category images */
 $all_categories = get_categories('taxonomy=product_cat&hide_empty=0&hierarchical=1');
 foreach ($all_categories as $cat) {
 $category = $cat->term_id; // retreives the currently queried object
 $category_thumbnail = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
 $image = wp_get_attachment_url($category_thumbnail);
 echo '<a href="'.get_term_link($cat->slug, 'product_cat').'"><img src="'.$image.'" width="350" height="150"
 alt="category-image"></a>';
 }
<pre>

Getting content from one site and inserting to our database in PHP


               include 'simple_html_dom.php'; // plugin for html parser with php
		$curl = curl_init(); // initializing command line tool url
		curl_setopt ($curl, CURLOPT_URL, "urlname"); // setting up to the target url
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_REFERER, "urlname");

		$result = curl_exec ($curl);
		curl_close ($curl);
		//write contents of $result to file
		$File = "video_show.html";
		$fh = fopen($File, 'w') or die("can't open file");
		fwrite($fh, $result);
		fclose($fh);
		$page = file_get_html("video_show.html"); 	//turn file into dom object
		$header = $page-&gt;find('table');

XML DOM PARSER


</pre>
<?php

*/ $url = 'urlname';
 $ch = curl_init();
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // setting option for url retrival
 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
 curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
 $raw_data = curl_exec ($ch);
 curl_close ($ch);
 $xml = simplexml_load_string($raw_data); // loads xml content
 for($i=0; $i<1;$i++) {
 $data = $xml->channel->item[$i]; // for counting each item content from server
 $name = $data->title;
 $link = $data->link;
 $description = $data->description;
 $content = $data->content;
 ?>
<!-- <div class='blog_link'><a href='<?php echo $link; ?>'><?php echo $name; ?></a></div>-->
<?php echo "<div class='blog_description'>".$description.$content."</div>";?>
 <div class='blog_more'><a href='<?php echo $link; ?>'>Read More</a></div>
<?php } ?>
<pre>

PHP HTML Dom Parser


		$counter = 0;
		foreach($header as $url) {
			if ($counter&gt;1) { // counter has set if content is empty/null. Here if counter greater than one contents will be displayed too.
					$item['image'] = $url-&gt;find('a',0)-&gt;href;		
					$item['airline'] = $url-&gt;find('span',0)-&gt;plaintext;
					$item['flight'] = $url-&gt;find('span',1)-&gt;plaintext;
					$item['ac'] = $url-&gt;find('span',2)-&gt;plaintext;
					$item['arrived'] = $url-&gt;find('span',3)-&gt;plaintext;
					$item['time'] = $url-&gt;find('span',4)-&gt;plaintext;
					$item['expected'] = $url-&gt;find('span',5)-&gt;plaintext;
					$item['remark'] = $url-&gt;find('span',6)-&gt;plaintext;			
					$a[] = $item; 
					// inserting content from web page to database after clearing HTML
						$query = "INSERT INTO live_flight (image, airline, flight, ac, arrived, time, expected, remark)
									VALUES('".$item['image']."',
										   '".$item['airline']."',
											'".$item['flight']."',
											'".$item['ac']."',
											'".$item['arrived']."',
											'".$item['time']."',
											'".$item['expected']."',
											'".$item['remark']."')
								";
						$result = mysql_query($query);
}
			while($row = mysql_fetch_array($result1)) {			
					$str = str_replace('&nbsp;', '', $row['remark']);				
					$sr = str_replace('On Time', '',$str);									
					$s = str_replace('Delay', 'late&nbsp;by', $sr);
					$ar = explode(&quot; &quot;, $s);
					$me = strtolower($row['arrived']);
					$mea = ucwords($me);
					$z = $row['image'];
					echo &quot;<tr>";
					echo  "<td>"."<a href='$z'>".$mea."</a>"."</td>".
						  "<td>".$row['time']." "."</td>".
						  "<td>".$ar[0]. ' '.human_time($ar[1])."</td>";
					echo "</tr>"; 					 	
			}