Archive for 'September, 2008'

How to remove an extension from a filename

Posted on September 4, 2008, Filled under PHP,  Bookmark it

If you need for any reason to remove an extension from a filename (string) this snippet can help you. An extension in this case is everything after the last period from the whole string.

<?php
/*
Source: Bit Repository (http://www.bitrepository.com/)
*/
function remove_filename_extension($filename)
{
$extension = strrchr($filename, ".");

$filename = substr($filename, 0, -strlen($extension));

return $filename;
}

$filename = 'This-is-a-photo-description.jpeg';

$str = remove_filename_extension($filename);

echo $str;
?>

Extract URL(s) from Link(s) with PHP

Posted on September 4, 2008, Filled under PHP,  Bookmark it

This is a script which extracts URLs from Links. The function gets the content from the HREF attribute and ignores the non-urls like: “javascript: openWindow()”.

Using Regular Expressions

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

$url = 'http://www.php.net/';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the urls from links

$links_regex = '/<a[^/>]*'.

'href=[\"|\']([^javascript:].*)[\"|\']/Ui';

preg_match_all($links_regex, $string, $out, PREG_PATTERN_ORDER);

echo "<pre>"; print_r($out); echo "</pre>";

function FetchPage($path)
{
$file = fopen($path, "r"); 

if (!$file)
{
exit("The was a connection error!");
} 

$data = '';

while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data;
}
?>

Shorten a string (text)

Posted on September 3, 2008, Filled under PHP,  Bookmark it

This is a function which shortens a text. For instance, this is useful when you want to display in a page excerpts from your articles (with the link to the full article).

<?php
function shorten_string($string, $chars, $end = '...', $strip_tags = true)
{
$string = ($strip_tags) ? strip_tags($string) : $string;
$str = substr($string, 0, $chars).$end;

return $str;
}

$text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Maecenas ac nulla at neque consectetuer semper.
Pellentesque et quam at justo rhoncus accumsan.
Nullam posuere, nisl eu porta dignissim, massa dolor porttitor mi,
at consequat mi ante in tortor.';

/*
1st argument: your text
2nd argument: how many chars to show from the string's beginning
3rd argument: the ending string (i.e. ... [more] [continue])
4th argument: Strip Tags (true or false)
*/

$excerpt = shorten_string($text, 100, '...[More]', true);

echo $excerpt;
?>

NOTE: The 3rd argument can be used as a link to your full article like: …<a href=”http://www.mydomain.com/article_page”>[More]</a>

PHP: Practical cURL function

Posted on September 2, 2008, Filled under PHP,  Bookmark it

A common cURL function that can be used for multiple purposes:

<?php
function LoadCURLPage($url, $agent = '', $cookie = "", $referer = "",
$post_fields = "", $return_transfer = 1, $follow_location = 1)
{
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);

if($agent)
{
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
}

if($post_fields)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if($referer)
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}

if($cookie)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}

$result = curl_exec ($ch);

curl_close ($ch);

return $result;
}

### Usage Examples ####

/* LOGIN (POST Method) */

$login_url = 'http://www.domain.com/login';

// Login
$post_info = array("username" => "username_here",
                          "password"   => "password_here");

$post_data = '';

foreach($post_info as $name => $value)
{
$post_data .= $name.'='.$value.'&';
}

$post_data = trim(substr($post_data, 0, -1));

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
 Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = 'temp/cookie.txt';
$result = LoadCURLPage($login_url, $agent, $cookie_file_path,
 $login_page, $post_data);

echo $result;

/* Fetch a web page (GET Method) */

$result = LoadCURLPage('http://www.yahoo.com/', $agent);

echo $result;
?>

Most Visited and Commented Post from BitRepository.com

The aim of this tutorial is to help you to create a simple (tableless) contact form using AJAX, JQuery & PHP. We will have a HTML page which will contain the form, a CSS file, a php page where the data will be sent and another file where the validation function(s) will be located. JQuery is a new and powerful library which simplifies the way that you write JavaScript.

Download | View Demo

Step 1 – Creating the configuration file

config.php

<?php
// To
define("WEBMASTER_EMAIL", 'your_name@domain.com');
?>

Here you should fill the e-mail address where you wish to receive the mail as well as the address where you wish to receive the replies (usually the same).

Step 2 – Creating the css file

style.css

/*
Credits: Bit Repository
CSS Library: http://www.bitrepository.com/
*/

html, body  { padding: 0; border: 0px none; }

.notification_error
{
border: 1px solid #A25965;
height: auto;
width: 90%;
padding: 4px;
background: #F8F0F1;
text-align: left;
-moz-border-radius: 5px;
}

.notification_ok
{
border: 1px #567397 solid;
height: auto;
width: 90%
padding: 8px;
background: #f5f9fd;
text-align: center;
-moz-border-radius: 5px;
}

.info_fieldset { -moz-border-radius: 7px; border: 1px #dddddd solid; }

.info_fieldset legend
{
border: 1px #dddddd solid;
color: black; 

font: 13px Verdana;

padding: 2px 5px 2px 5px;
-moz-border-radius: 3px;
}

.button
{
border: 1px solid #999999;
border-top-color: #CCCCCC;
border-left-color: #CCCCCC; 

background: white;

color: #333333; 

font: 11px Verdana, Helvetica, Arial, sans-serif;

-moz-border-radius: 3px;
}

/* Label */
label {width: 140px; padding-left: 20px; margin: 5px; float: left; text-align: left;}

/* Input, Textarea */
input, textarea
{
margin: 5px;
padding: 0px;
float: left;

border: 1px solid #999999;
border-top-color: #CCCCCC;
border-left-color: #CCCCCC; 

color: #333333; 

font: 11px Verdana, Helvetica, Arial, sans-serif;

-moz-border-radius: 3px;
}

/* BR */

br { clear: left; }

Read more from this entry…

Hi,

In this tutorial you will see some usage examples of the function range(). They are very effective when you need to create a range of elements. For instance let’s say you need to list some content using an alphabetical search. Here’s a way to do it:

<?php
/*
Alternative array

array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
*/

$non_alpha = array('#');
$alphabetical_array = range('a','z'); 

$final_array = array_merge($non_alpha, $alphabetical_array);

function AddLinks($letter)
{
$title = strtoupper($letter);
$a = "<a href='alpha_search.php?letter=".$letter."'>".$title."</a>";
return($a);
}

// Use array_map to modify the array by adding the links

$format_array = array_map("AddLinks", $final_array);

echo implode(" | ", $format_array);
?>

Our code will nicely output the alphabetical navigation links with the URL for each letter:

php-alphabetical-chars

Other examples of range() usage:

<?php
// 1, 2, 3, 4, 5, 6 ... 30

$sample_one = range(1,30);

echo implode(", ", $sample_one)."<br />";

/*
Start with 50, up to 200 (increment by 10)
50, 60, 70, 80, 90 ... 200
*/

$sample_two = range(50, 200, 10); 

echo implode(", ", $sample_two)."<br />";

// 20, 19, 18, 17, 16 .. 3, 2, 1
$sample_three = range(20, 1); 

echo implode(", ", $sample_three)."<br />";
?>

Note: This range() function returns elements from low to high, but if low > high then the sequence will be from high to low.

Greetings,

This article is about creating a form without using any tables, which can be easily customized and used in your own site. Let’s start creating the necessary files: tableless_form.html & style.css. You can choose between two structures: one using DIVs & another one using labels.

Step 1: Creating the CSS file

We will create and place the CSS file in the same folder where the tableless_form.html file is located. Here’s the file’s content:

First Method (with DIVs)

html, body
{
padding: 0;
border: 0px none;
}

/* Let's add some style to our fieldset & legend */

fieldset
{
-moz-border-radius: 7px;
border: 1px #dddddd solid;
padding: 10px;
width: 550px;
margin-top: 10px;
}

fieldset legend
{
border: 1px #1a6f93 solid;
color: black; 

font-family: Verdana;
font-weight: none;
font-size: 13px;

padding-right: 5px;
padding-left: 5px;
padding-top: 2px;
padding-bottom: 2px;

-moz-border-radius: 3px;
}

/* Main DIV */
.m
{
width: 560px;
padding: 20px;
height: auto;
}

/* Left DIV */
.l
{
width: 140px;
margin: 0px;
padding: 0px; 

float: left;
text-align: right;
}

/* Right DIV */
.r
{
width: 300px;
margin: 0px;
padding: 0px; 

float: right; 

text-align: left;
}

.a
{
clear: both;
width: 470px;
padding: 10px;
}

Second method (With Labels)

HTML, BODY
{
padding: 0;
border: 0px none;
}

/* Let's add some style to our fieldset & legend */

fieldset
{
-moz-border-radius: 7px;
border: 1px #dddddd solid;
padding: 10px;
width: 550px;
margin-top: 10px;
}

fieldset legend
{
border: 1px #1a6f93 solid;
color: black; 

font-family: Verdana;
font-weight: none;
font-size: 13px;

padding-right: 5px;
padding-left: 5px;
padding-top: 2px;
padding-bottom: 2px;

-moz-border-radius: 3px;
}

/* Label */
label
{
width: 140px;
padding-left: 20px;
margin: 5px;
float: left;
text-align: left;
}

/* Input, Textarea */
input, textarea
{
margin: 5px;
padding: 0px;
float: left;
}

/* BR */

br
{
clear: left;
}


Read more from this entry…