Thursday, September 20, 2012

Make clickable text to links: PHP

With this function you can make clickable text to links.
Function:

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

function dolinks($text)
{

$text = html_entity_decode($text);
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1\\2', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'\\1', $text);
return $text;
}

// Example Usage
echo dolinks("This is a test clickable link: http://4evertutorials.blogspot.in/ You can also try using an email address like username@example.com");

?>

Wednesday, September 19, 2012

RSS Reader Class in PHP

What is RSS?
RSS stands for Really Simple Syndication or Rich Site Summary. RSS is used by (among other things) news websites, weblogs and podcasting. RSS feeds provide web content, or summaries of web content, together with links to the full versions of the content. RSS delivers this information as an XML file called an RSS feed, webfeed, RSS stream, or RSS channel. In addition to facilitating syndication, RSS feeds allow a website's frequent readers to track updates on the site as soon as they become available using an aggregator. The aggregator provides a consolidated view of the content in a single browser display or desktop application. Such aggregators or applications are also referred to as RSS readers, feed readers, feed aggregators or news readers.

Universal RSS Reader Class:

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
class RSSReader {
var $xml = null;
var $pos = 0;
var $count = 0;

function __construct($feed_url) {
$this -> load_url($feed_url);
}

function load_url($feed_url) {
$this -> load_string(file_get_contents($feed_url));
}

function load_string($feed_string) {
$this -> xml = simplexml_load_string(str_replace('content:encoded', 'content_encoded', $feed_string));
$this -> pos = 0;
$this -> count = count($this -> xml -> channel -> item);
}

function get_title() {
return $this -> xml -> channel -> title;
}

function get_link() {
return $this -> xml -> channel -> link;
}

function get_pubdate() {
return $this -> xml -> channel -> pubdate;
}

function hasNext() {
return $this -> count > $this -> pos;
}

function next() {
$obj = $this -> xml -> channel -> item[$this -> pos++];
return array(
'title' => (string) $obj -> title,
'link' => (string) $obj -> link,
'description' => (string) $obj -> description,
'content' => (string) $obj -> content_encoded,
'pubDate' => strtotime($obj -> pubDate),
);
}
}?>
Usage:
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
$rss = new RSSReader('http://news.google.com/?output=rss');
while ($rss -> hasNext())
print_r($rss -> next());
?>

Thursday, September 6, 2012

Send a magic packet over the Internet with PHP

PHP function to send a magic packet over the Internet
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
flush();
function WakeOnLan($addr, $mac,$socket_number) {

$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
return FALSE;
}
else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret <0) {
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return FALSE;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
}
else {
echo "Magic packet failed!";
return FALSE;
}
}
}

// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "9";
// MAC Address of the listening computer's network device
$mac_addy = "00:01:02:03:04:05"; 
// IP address of the listening computer. Input the domain name if you are using a hostname (like when under Dynamic DNS/IP)
$ip_addy = gethostbyname("my.computer.com");
WakeOnLan($ip_addy, $mac_addy,$socket_number)
?>

Composition of Variables


Amazing trick by PHP
You can use composition of variables. I don’t how it can be useful, but the following code is valid:
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

${‘a’ . ‘b’} = ‘c’;
echo $ab; // it will output c

?>

Recursive Directory Delete Function

Recursive Directory Delete Function
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
define('PATH', '/www/public/images/');

function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("couldn't delete $dir$file
");
}
else
unlink($dir.$file) or DIE("couldn't delete $dir$file
");
}
}
closedir($mydir);
}
destroy(PATH);
echo 'all done.';

?>
 

© 2014 4everTutorials. All rights resevered.

Back To Top