I have a server but it only has PHP 5 on it at the moment I have a broken arm so moving (just typing this) is a killer.
I thought all the class_exists and function_exists functions built into PHP would at least be backward compatible however as PHP seems to just remove functionality unlike other languages without handling it nicely I am running into problems with the removal of the \e modifier in regular expressions.
For example I had this regular expression to decapitalise words after full stops if they are single words e.g "Hello Rob Reid. Said the bar man" would be "Hello Rob Reid. said the bar man" - do not worry about grammar the dots are used later as placeholders.
So this was working in PHP 5
$content = preg_replace("/(\.[”’\"]?\s*[A-Z][a-z]+\s[a-z])/e","strtolower('$1')",$content);
But in PHP 7 it doesn't and \e modifier has been removed.
So in PHP 7 I have to do this.
$content = preg_replace_callback("@(\.[”’\"]?\s*[A-Z][a-z]+\s[a-z])@",
function ($matches) {
return strtolower($matches[0]);
},
$content);
However I need code that will work whether the user has PHP5 or PHP7 and I cannot force them to upgrade I just need to determine which branch to use without the parser throwing errors.
I thought a simple error_reporting(0);
and function_exists("preg_replace_callback");
would work but it doesn't seem to.
I have tried online PHP testers like this one that evaluates the code.
However I get parse errors, even if I put error_reporting(0); at the top of the script.
I thought a function to see if a function exists and is callable would work like the one below but to no avail.
I thought a combo of function_exists and is_callable would work.
function is_function($name)
{
$ret = true;
if(function_exists($name) && is_callable($name)){
return $ret;
}else{
$ret = false;
}
echo "return " . intval($ret) . " from is_function\n";
return $ret;
}`
So how can I branch the code in the main function depending on what functions (or PHP version) is supported?
You can copy this whole bit of code into the sandbox tester and run it...
<?php
error_reporting(0);
echo "start\n";
$content = "Hello Rob Reid said the bar man. He walked away.\n\n";
echo $content;
if(is_function("preg_replace_callback")){
echo "function does exist\n";
@$content = preg_replace_callback("@(\.[”’\"]?\s*[A-Z][a-z]+\s[a-z])@",
function ($matches) {
return strtolower($matches[0]);
},
$content);
}else{
echo "function does not exist\n";
$content = preg_replace("/(\.[”’\"]?\s*[A-Z][a-z]+\s[a-z])/e","strtolower('$1')",$content);
}
function is_function($name)
{
$ret = true;
if(function_exists($name) && is_callable($name)){
return $ret;
}else{
$ret = false;
}
echo "return " . intval($ret) . " from is_function\n";
return $ret;
}
echo "new content\n\n";
echo $content . "\n\n";
echo "stopped";
and watch the output change as it hits PHP 5.217 then you get errors e.g
Parse error: syntax error, unexpected T_FUNCTION in [...][...] on line 11
So any ideas how to branch versions of PHP in the same class file seeing they couldn't keep the /e modifier or handle the new callbacks>
Much thanks for any help.