Useful string processing functions in PHP
function get_string_between($string, $start, $end) {
$string = " " . $string;
$ini = strpos($string, $start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
function get_array_strings_between($string, $start, $end) {
$arr = array();
$arr2 = array();
$arr3 = array();
$arr = explode($start, $string);
for($i = 1; $i < count($arr); $i++) {
$arr2[$i] = explode($end, $arr[$i]);
}
for($i = 1; $i <= count($arr2); $i++) {
$arr3[$i-1] = $arr2[$i][0];
}
return $arr3;
}
function file_get_contents_https($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function remove_wierd($Str) {
$StrArr = str_split($Str); $NewStr = '';
foreach ($StrArr as $Char) {
$CharNo = ord($Char);
if ($CharNo == 163) { $NewStr .= $Char; continue; } // keep £
if ($CharNo > 31 && $CharNo < 127) {
$NewStr .= $Char;
}
}
return $NewStr;
}
function file_get_put_image($url, $save_img) {
$ch = curl_init($url);
$fp = fopen($save_img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
function echo_textarea($x, $rows=20, $cols=70) {
echo '<textarea cols="'.$cols.'" rows="'.$rows.'">'.$x.'</textarea><br /><br />';
}
function print_r_textarea($x, $rows=20, $cols=70) {
echo '<textarea cols="'.$cols.'" rows="'.$rows.'">'; print_r($x); echo '</textarea><br /><br />';
}
function pt($x, $rows=20, $cols=70) {
echo '<textarea cols="'.$cols.'" rows="'.$rows.'">'; print_r($x); echo '</textarea><br /><br />';
}
function get_web_page( $url ) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
// return $header;
return $content;
}
function clean_get_string_between($str, $start, $end) {
return trim(strip_tags(get_string_between($str, $start, $end)));
}
function match_found($needles, $haystack) {
foreach($needles as $needle) {
if (strpos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
function match_all_found($needles, $haystack) {
if(empty($needles)) {
return false;
}
foreach($needles as $needle) {
if (strpos($haystack, $needle) == false) {
return false;
}
}
return true;
}
function match_one($str, $sub) {
if (strpos($str, $sub) !== false) {
return 'true';
}
return false;
}
function get_float($str) {
return filter_var( $str, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
}
function get_number($str) {
return filter_var($str, FILTER_SANITIZE_NUMBER_INT);
}
// ***** Other Functions *****
function wordwrap_ucwords_trim($x, $wrap_length) {
return wordwrap(ucwords(strtolower(trim($x))), $wrap_length, '<br />');
}
function wordwrap_ucase_trim($x, $wrap_length) {
return wordwrap(strtoupper(strtoupper(trim($x))), $wrap_length, '<br />');
}
function remove_period($x) {
return trim($x, ".");
}
function download_file( $fullPath ){
// Must be fresh start
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
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=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath );
} else
die('File Not Found');
}
function pad_titles_numbers($title_string, $number_string, $length, $num_length, $char = " ") {
$fill_length = $length - ( strlen($title_string) + strlen($number_string) );
return $title_string . ' ' . str_repeat($char, $fill_length) . ' ' . str_pad( $number_string, $num_length, " ", STR_PAD_LEFT);
}
function lr_trim_array($arr, $l_str, $r_str) {
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = trim($arr[$i]);
$arr[$i] = ltrim($arr[$i], $l_str);
$arr[$i] = rtrim($arr[$i], $r_str);
}
return $arr;
}
function prepend_array_str($arr, $str) {
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = $str . $arr[$i];
}
return $arr;
}
function append_array_str($arr, $str) {
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = $arr[$i] . $str;
}
return $arr;
}
function ucfirst_array($arr) {
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = ucfirst($arr[$i]);
}
return $arr;
}
function ucwords_array($arr) {
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = ucwords($arr[$i]);
}
return $arr;
}
function remove_duplicate_words($s) {
return implode(' ', array_unique(explode(' ', $s)));
}