PHP Code Methods Online
Code of the cleanLongWords() function
Use of the cleanLongWords() subroutine to make a code with a set of alphanumeric characters are used for different purposes as required. The maximum word length judged is one which has a random generation of characters and words chosen in making of the set of characters or code resultantly.
function cleanLongWords($text, $nrchr=2, $maxword=23) {
// function to clean text with multiple consecutive characters and very long words ( http://coursesweb.net/ )
// $nrchr = allowed number of consecutive character
// $maxword = maximum word length
$paterns = array();
$chr = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l',';','z','x','c','v','b','n','m',',','!','@','#','%','&','_','=',':','"','`','~',';','â','á','é','í','ó','ú','ý','ø','č','ć','đ','š','ž','ā','ä','ǟ','ḑ','ē','ī','ļ','ņ','ō','ȯ','ȱ','õ','ȭ','ŗ','š','ț','ū','ž','ş','î','ă',"'",'$','\^','\*','\(','\)','\{','\}','\|','\?','\.','\[','\]','\/','\\\\','\>','\<');
// uncomment next line if you want to clean consecutive numbers too
/// $chr = array_merge($chr, array('1','2','3','4','5','6','7','8','9','0'));
$n_chr = count($chr);
for($i=0; $i<$n_chr; $i++) {$paterns[$i] = '/(['. $chr[$i] .']{'. $nrchr .',}){2,}/i'; }
$text = preg_replace($paterns, '$1', $text);
// if $maxword > 0, split the word to specified number of characters
return ($maxword > 0) ? wordwrap($text, $maxword, ' ', true) : $text;
}
<?php
// Here Add the cleanLongWords() function
$text = '\\\\\\\\\ A ttttttteeeeeeeeexxxxxxt with loooooonnnnnngggggg woooooooooooords and coooooonnnnnnsssssseeeeeeccccccuuuuuuttttttiiiiiivvvvvveeeeeee characteeeeeerrrrrrssssss [[[[[]]]]]] ///////......';
$text1 = cleanLongWords($text);
$text2 = cleanLongWords($text, 3); // allow 3 consecutive repetitions of characters
$text3 = cleanLongWords($text, 1, 0); // not consecutive characters, and not split the word
echo '$text1 - '. $text1;
echo '<br>$text2 - '. $text2;
echo '<br>$text3 - '. $text3;
?>
PHP Coding and Methodology
Interpret, Parse, Replace variables in string using valuePhp-mysql CourseHome, HTML, CSS, JavaScript. You can replace variables in string using values defined in php development arpatech and you can also use the strtr() or preg_replace_callback() function. Make sure there is a particular pattern you are aware of and you are interested in making a choice of which course you are to take and the one that suits you. The code snippet provided below does just that.
Example with strtr():
<?php
// array with data for the variables added in string
$data = array(
'{$site}'=>'http://coursesweb.net/',
'{$year}'=>date('Y')
);
$str = 'Code-snippets from {$site} , added in: {$year}.';
$str2 = strtr($str, $data);
echo $str2; // Code-snippets from http://coursesweb.net/ , added in: 2014.
Example with preg_replace_callback()
- Click to select it.
<?php
// array with data for the variables added in string
$data = array(
'id'=>'test2',
'domain'=>'CoursesWeb.net'
);
$str = '<div id="{$id}">Parse this string, interpret {$domain} </div>';
$str2 = preg_replace_callback('/{\$([^}]+)}/', function($m) {
GLOBAL $data;
return $data[$m[1]];
}, $str);
echo $str2; // <div id="test2">Parse this string, interpret CoursesWeb.net </div>
PHP Code and Purpose
Force Download files using PHPPhp-mysql CourseHome, HTML, CSS, and JavaScript. The downloadFile() function given in the page are usable and a forced download of various type of files are made using primarily PHP in: csv, doc, html, jpg, pdf, png, ppt, xls, xml, zip, etc.
The downloadFile() function
// function to download files with php ( http://coursesweb.net/php-mysql/ )
// receives the path and filename to download
function downloadFile($file) {
$ar_ext = explode('.', $file);
$ext = strtolower(end($ar_ext));
$extensions = array(
'bmp' => 'image/bmp',
'csv' => 'text/csv',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'exe' => 'application/octet-stream',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'ico' => 'image/vnd.microsoft.icon',
'jpeg' => 'image/jpg',
'jpe' => 'image/jpg',
'jpg' => 'image/jpg',
'pdf' => 'application/pdf',
'png' => 'image/png',
'ppt' => 'application/vnd.ms-powerpoint',
'psd' => 'image/psd',
'swf' => 'application/x-shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'xhtml' => 'application/xhtml+xml',
'xml' => 'application/xml',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'zip' => 'application/zip'
);
$ctype = isset($extensions[$ext]) ? $extensions[$ext] : 'application/force-download';
if (file_exists($file) && is_readable($file)) {
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
header('Pragma: public'); // required
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false); // required for certain browsers
header('Content-Type: '. $ctype);
header('Content-Disposition: attachment; filename='. $file .';' );
header('Content-Transfer-Encoding: binary');
header('Content-Length: '. filesize($file));
readfile($file);
}
else {
header('HTTP/1.0 404 Not Found');
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
}
<?php
// HERE ADD THE downloadFile() function
$dir = 'download/'; // folder wth files for download
// $_GET['file'] contains the name and extension of the file stored in 'download/'
if (isset($_GET['file'])) {
$file = $dir . strip_tags($_GET['file']);
downloadFile($file);
}
?>