Monthly Archives: July 2010

Alternative to explode function (PHP)

When you have a set of string in an array or strings separated by comma (or any other character), you can use these two alternative. I prefer the second one 🙂

1
2
3
4
5
6
7
8
9
10
 
//code with explode
$list = explode(",",$astring);
foreach ($astring as $value) {
  echo $value;
  echo "<br />";
}
 
//code with str_replace
echo str_replace(",", "<br />", $astring);

Some technical SEO tips

Worth to consider when building your website to make it search engine friendly.

Important SEO HTML tags

  • title
  • h1
  • h2
  • h3
  • b
  • strong
  • img alt
  • a href title and keyword in achor text

Use http://www.yourdomain.com/ (always with trailing slash at back)

.htaccess redirect to redirect http://yourdomain.com to http://www.yourdomain.com (site-wide)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.yourdomain.com/$1 [L,R=301]

Free sitemap at xml-sitemaps.com

Standard sitemap name/location – yourdomain.com/sitemap.xml

Robots.txt syntax

User-agent: *
Disallow: /privatefolder/
Disallow: /privatefile.html
Sitemap: http://www.yourdomain.com/sitemap.xml

Function to create and read csv file

Found this function from bin-co. Haven’t tested the function but I think it might be useful for me in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Get the result of the query as a CSV stream.
//http://www.bin-co.com/php/scripts/csv_import_export/
function CSVExport($query) {
    $sql_csv = mysql_query($query) or die("Error: " . mysql_error()); //Replace this line with what is appropriate for your DB abstraction layer
 
    header("Content-type:text/octect-stream");
    header("Content-Disposition:attachment;filename=data.csv");
    while($row = mysql_fetch_row($sql_csv)) {
        print '"' . stripslashes(implode('","',$row)) . "\"\n";
    }
    exit;
}
 
//Import the contents of a CSV file after uploading it
//http://www.bin-co.com/php/scripts/csv_import_export/
//Aruguments : $table - The name of the table the data must be imported to
//                $fields - An array of fields that will be used
//                $csv_fieldname - The name of the CSV file field
function CSVImport($table, $fields, $csv_fieldname='csv') {
    if(!$_FILES[$csv_fieldname]['name']) return;
 
    $handle = fopen($_FILES[$csv_fieldname]['tmp_name'],'r');
    if(!$handle) die('Cannot open uploaded file.');
 
    $row_count = 0;
    $sql_query = "INSERT INTO $table(". implode(',',$fields) .") VALUES(";
 
    $rows = array();
 
    //Read the file as csv
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $row_count++;
        foreach($data as $key=>$value) {
            $data[$key] = "'" . addslashes($value) . "'";
        }
        $rows[] = implode(",",$data);
    }
    $sql_query .= implode("),(", $rows);
    $sql_query .= ")";
    fclose($handle);
 
    if(count($rows)) { //If some recores  were found,
        //Replace these line with what is appropriate for your DB abstraction layer
        mysql_query("TRUNCATE TABLE $table") or die("MySQL Error: " . mysql_error()); //Delete the existing records
        mysql_query($sql_query) or die("MySQL Error: " . mysql_error()); // and insert the new ones.
 
        print 'Successfully imported '.$row_count.' record(s)';
    } else {
        print 'Cannot import data - no records found.';
    }
}