
Download for free demo version of
Screen Tool:
screen capturing software for
Windows,
Linux (
static) or
MacOSX.
It will let you easily capture screen,
make annotations
and send it through Skype or save it.
For more details and shop click
here.
Questions/Answers regarding php code.
I made few scripts that use file_get_contents() and file_put_contents() functions, but now need to install them on server that doesn't support php5. It will take me too much time to change it everywhere in the code. What to do?
Use these function replacements for php < 5. They do not work exactly like php5 functions (no flag parameters, no resource context parameter):
if (!function_exists("file_put_contents")) {
function file_put_contents($filename,$text) {
$fp = fopen($filename, "w");
if ($fp) {
flock($fp,2);
if (is_array($text)) $text=implode("",$text);
fputs($fp,$text);
flock($fp,3);
fclose($fp);
}
return filesize($filename);
}
}
if (!function_exists("file_get_contents")) {
function file_get_contents($filename) {
$ret=file($filename);
if ($ret!==false) $ret=implode("",$ret);
return $ret;
}
}
You need to
login to be able to post your answer.
How can I test does my server supports mod_rewrite?
The easiest way to test is mod_rewrite loaded is to do it with php function apache_get_modules():
if (in_array("mod_rewrite", apache_get_modules())) {
echo "mod_rewrite OK";
} else {
echo "mod_rewrite NOT OK";
}
But this won't work if you use php version less then 5 and Apache version less then 2. So, to be sure add this code to above one, also:
if (!function_exists("apache_get_modules")) {
function apache_get_modules() {
ob_start();
phpinfo();
$info=ob_get_clean();
$find=preg_match("/<td[^><]*>\\s*Loaded Modules[^><]*<\\/td>\\s*<td[^><]*>([^><]*)<\\/td>/",$info,$modules);
$split=",";
$modules=$modules[1];
if (strpos($modules,",")===false) {
$split=" ";
} else {
$modules=str_replace(" ","",$modules);
}
$modules=explode($split,$modules);
return $modules;
}
}
You need to
login to be able to post your answer.
I need case insensitive replace, but my hoster doesn't have PHP 5. How can I make case insesitive replace with PHP 4?
Here is function that you can use. It is exact replacement for str_ireplace function in PHP 5:
if (!function_exists("str_ireplace")) { // check does this function already exist
function str_ireplace($search,$replace,$subject,&$count) {
$count=0;
$func="str_ireplace";
if (is_array($subject)) {
$ret=array();
foreach ($subject as $key=>$element) {
$ret[$key]=$func($search,$replace,$element,$c);
$count+=$c;
}
return $ret;
} else {
if (is_array($search)) {
$ret=$subject;
foreach (array_keys($search) as $key) {
if (is_array($replace)) {
$rep="";
if (isset($replace[$key])) $rep=$replace[$key];
} else {
$rep=$replace;
}
$ret=$func($search[$key],$rep,$ret,$c);
$count+=$c;
}
return $ret;
} else {
$ret=$subject;
$pos=true;
$lastpos=0;
while ($pos) {
$pos=strpos(strtolower($subject),strtolower($search),$lastpos);
if ($pos!==false) {
$ret=substr($ret,0,$pos).$replace.substr($ret,$pos+strlen($search));
$count++;
$lastpos=$pos+strlen($replace);
}
}
return $ret;
}
}
}
}
You need to
login to be able to post your answer.
How can I parse string containing javascript code?
Here is function that will parse string containing JS script code and return array of javascript source rows:
function explode_js($string) {
$startquotes=array("'",'"',"(");
$endquotes=array("'",'"',")");
$explodeby=";";
$ret=array();
$quote=false;
$s=array();
for ($i=0; $i<strlen($string); $i++) {
$s[]=substr($string,$i,1);
}
$count=0;
$inquotes=false;
$quotedInBrackets=false;
foreach ($s as $char) {
if (!$quote) {
$tmpQuote=array_search($char,$startquotes);
if ($tmpQuote!==false) {
$quote=$tmpQuote;
$inquotes=true;
}
} else {
if ($quote==2) {
if (!$quotedInBrackets) {
$tmpQuote=array_search($char,$startquotes);
if ($tmpQuote!==false) {
if ($tmpQuote!=2) $quotedInBrackets=$tmpQuote;
}
} else {
if ($char==$endquotes[$quotedInBrackets]) $quotedInBrackets=false;
}
}
if (!$quotedInBrackets AND $char==$endquotes[$quote]) {
$quote=false;
$inquotes=false;
}
}
if (!$inquotes AND $char==$explodeby) {
$count++;
} else {
$ret[$count].=$char;
}
}
return $ret;
}
You need to
login to be able to post your answer.