Tutorial Convert Comma Separated String into Array
Easy way to turn a CSV file into a parseable array.
<?php
$str="foo,bar,baz,bat";
$arr=explode(",",$str);
// print_r($arr);
?>
Easy way to turn a CSV file into a parseable array.
<?php
$str="foo,bar,baz,bat";
$arr=explode(",",$str);
// print_r($arr);
?>
function br2newline( $input ) {
$out = str_replace( "<br>", "\n", $input );
$out = str_replace( "<br/>", "\n", $out );
$out = str_replace( "<br />", "\n", $out );
$out = str_replace( "<BR>", "\n", $out );
$out = str_replace( "<BR/>", "\n", $out );
$out = str_replace( "<BR />", "\n", $out );
return $out;
}
Converts a break tag to a newline - no matter what kind of HTML is being processed.
function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}
For instance, if you want to use a string as part of a URL but need to make it safe for that kind of use.
function replace_accents($str) {
$str = htmlentities($str, ENT_COMPAT, "UTF-8");
$str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
return html_entity_decode($str);
}
<?php
/*
This code by Chris Coyier
*/
$i = 0; // counter to be used later;
?>
Comments in PHP can be between /* */ markings (useful for multiple line comments) or after // markings (for single lines only).
Variables that are submitted via web forms always need to be cleaned/sanitized before use in any way, to prevent against all kinds of different malicious intent.
function clean($value) {
// If magic quotes not turned on add slashes.
if(!get_magic_quotes_gpc())
// Adds the slashes.
{ $value = addslashes($value); }
// Strip any tags from the value.
$value = strip_tags($value);
// Return the value out of the function.
return $value;
}
$sample = "<a href='#'>test</a>";
$sample = clean($sample);
echo $sample;
Performs a cURL-Request to check, if a website exists / is online
<?php
if (isDomainAvailible('http://www.css-tricks.com'))
{
echo "Up and running!";
}
else
{
echo "Woops, nothing found there.";
}
//returns true, if domain is availible, false if not
function isDomainAvailible($domain)
{
//check, if a valid url is provided
if(!filter_var($domain, FILTER_VALIDATE_URL))
{
return false;
}
//initialize curl
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
//get answer
$response = curl_exec($curlInit);
curl_close($curlInit);
if ($response) return true;
return false;
}
?>
<?php
function Visit($url){
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,3);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
$page=curl_exec($ch);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300) return true;
else return false;
}
if (Visit("http://www.google.com"))
echo "Website OK"."n";
else
echo "Website DOWN";
?>
<?php
ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen("http://www.css-tricks.com","r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
echo("<span class='online'>Online</span>");
}
else {
echo("<span class='offline'>Offline</span>");
}
?>
If the file name exists, returns new file name with _number appended so you don't overwrite it.
function file_newname($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
$newname = $filename;
$counter = 0;
while (file_exists($newpath)) {
$newname = $name .'_'. $counter . $ext;
$newpath = $path.'/'.$newname;
$counter++;
}
return $newname;
}
myfile.jpg
myfile_0.jpg
myfile_1.jpg