Search code examples
regexzend-frameworkpreg-matchinternal-server-error

How to remove time and memory from 500 internal error - Zend


Actually 500 internal error is the combination of "Time-Memory-Function-Location".

Now I would like to remove Time and Memory from 500 Internal Error.

Actual Output:

#TimeMemoryFunctionLocation
10.0002365832{main}(  ).../http.php:0
20.05691577072Zend\Mvc\Application->run(  ).../http.php:48
30.06211677272Zend\EventManager\EventManager->triggerEventUntil(  ).../Application.php:332
40.06211677272Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
50.06221677912Zend\Mvc\DispatchListener->onDispatch(  ).../EventManager.php:322
60.06261682144WebService\Controller\User\ManageAccountController->dispatch(  ).../DispatchListener.php:138
70.06261682144WebService\Controller\User\ManageAccountController->dispatch(  ).../AbstractRestfulController.php:313
80.06261682464Zend\EventManager\EventManager->triggerEventUntil(  ).../AbstractController.php:106
90.06261682464Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
100.06281686864WebService\Controller\User\ManageAccountController->onDispatch(  ).../EventManager.php:322
110.07321939200WebService\Controller\User\ManageAccountController->onDispatch(  ).../AbstractController.php:129
120.07321939232WebService\Controller\User\ManageAccountController->processPostData(  ).../AbstractRestfulController.php:429
130.07341945960WebService\Controller\User\ManageAccountController->create(  ).../AbstractController.php:234
140.08922331024Model\Model\UserModel->updateUser(  ).../ManageAccountController.php:50
150.08932331464Model\Mapper\UserMapper->updateUserData(  ).../UserModel.php:560
160.08932332744Application\Database\Connection->execute(  ).../UserMapper.php:475
170.08942332840Zend\Db\Adapter\Adapter->query(  ).../Connection.php:77
180.08942244472Zend\Db\Adapter\Driver\Mysqli\Statement->prepare(  ).../Adapter.php:193

Expected Output:

{main}(  ).../http.php:0
Zend\Mvc\Application->run(  ).../http.php:48
Zend\EventManager\EventManager->triggerEventUntil(  ).../Application.php:332
Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
Zend\Mvc\DispatchListener->onDispatch(  ).../EventManager.php:322
WebService\Controller\User\ManageAccountController->dispatch(  ).../DispatchListener.php:138
WebService\Controller\User\ManageAccountController->dispatch(  ).../AbstractRestfulController.php:313
Zend\EventManager\EventManager->triggerEventUntil(  ).../AbstractController.php:106
Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
WebService\Controller\User\ManageAccountController->onDispatch(  ).../EventManager.php:322
WebService\Controller\User\ManageAccountController->onDispatch(  ).../AbstractController.php:129
WebService\Controller\User\ManageAccountController->processPostData(  ).../AbstractRestfulController.php:429
WebService\Controller\User\ManageAccountController->create(  ).../AbstractController.php:234
Model\Model\UserModel->updateUser(  ).../ManageAccountController.php:50
Model\Mapper\UserMapper->updateUserData(  ).../UserModel.php:560
Application\Database\Connection->execute(  ).../UserMapper.php:475
Zend\Db\Adapter\Adapter->query(  ).../Connection.php:77
Zend\Db\Adapter\Driver\Mysqli\Statement->prepare(  ).../Adapter.php:193

I have tried this regular expression. But i didn't get the solution

/[\d\.]*([a-zA-Z\.\/\\\->\(\) {}]*:\d*)/g 

Please help me to get the solution.


Solution

  • You could match 1 or more digits, then a dot and again 1 or more digits and assert what is on the right is either a pattern {...} or 1+ word characters followed by a \ to make it a bit more specific.

    ^\d+\.\d+(?={[^{}]*}|\w+\\)
    

    In the replacement use an empty string.

    Explanation

    • ^ Start of string
    • \d+\.\d+ Match 1+ digits, a dot and 1+ digits
    • (?= Positive lookahead, assert what is on the right is
      • {[^{}]*} Match { till } without matching { or } in between
      • | Or
      • \w+\\ Match 1+ word chars followed by \
    • ) Close lookahead

    Regex demo | Php demo

    If you also want to remove the #TimeMemoryFunctionLocation, you could make use of the \G anchor

    (?:#TimeMemoryFunctionLocation\R|\G)\d+\.\d+((?:{[^{}]*}|\w+\\).*\R*)
    
    • (?: Non capture group
      • #TimeMemoryFunctionLocation\R Match #TimeMemoryFunctionLocation and a unicode newline sequence
      • | Or
      • \G Assert position at the end of the previous match, which will in this case at the start of the string
    • ) Close group
    • \d+\.\d+ Match 1+ digits, a dot and 1+ digits
    • ( capture group 1
      • (?:{[^{}]*}|\w+\\)
      • .*\R* Match the rest of the line and 0+ unicode newline sequences
    • ) Close group 1

    Regex demo | Php demo

    In the replacement use capture group 1

    For example

    $re = '/(?:#TimeMemoryFunctionLocation\R|\G)\d+\.\d+((?:{[^{}]*}|\w+\\\\).*\R*)/';
    $str = "...";
    $result = preg_replace($re, '$1', $str);
    

    For example, using the first pattern:

    $re = '/^\d+\.\d+(?={[^{}]*}|\w+\\\\)/m';
    $str = '#TimeMemoryFunctionLocation
    10.0002365832{main}(  ).../http.php:0
    20.05691577072Zend\\Mvc\\Application->run(  ).../http.php:48
    30.06211677272Zend\\EventManager\\EventManager->triggerEventUntil(  ).../Application.php:332
    40.06211677272Zend\\EventManager\\EventManager->triggerListeners(  ).../EventManager.php:179
    50.06221677912Zend\\Mvc\\DispatchListener->onDispatch(  ).../EventManager.php:322
    60.06261682144WebService\\Controller\\User\\ManageAccountController->dispatch(  ).../DispatchListener.php:138
    70.06261682144WebService\\Controller\\User\\ManageAccountController->dispatch(  ).../AbstractRestfulController.php:313
    80.06261682464Zend\\EventManager\\EventManager->triggerEventUntil(  ).../AbstractController.php:106
    90.06261682464Zend\\EventManager\\EventManager->triggerListeners(  ).../EventManager.php:179
    100.06281686864WebService\\Controller\\User\\ManageAccountController->onDispatch(  ).../EventManager.php:322
    110.07321939200WebService\\Controller\\User\\ManageAccountController->onDispatch(  ).../AbstractController.php:129
    120.07321939232WebService\\Controller\\User\\ManageAccountController->processPostData(  ).../AbstractRestfulController.php:429
    130.07341945960WebService\\Controller\\User\\ManageAccountController->create(  ).../AbstractController.php:234
    140.08922331024Model\\Model\\UserModel->updateUser(  ).../ManageAccountController.php:50
    150.08932331464Model\\Mapper\\UserMapper->updateUserData(  ).../UserModel.php:560
    160.08932332744Application\\Database\\Connection->execute(  ).../UserMapper.php:475
    170.08942332840Zend\\Db\\Adapter\\Adapter->query(  ).../Connection.php:77
    180.08942244472Zend\\Db\\Adapter\\Driver\\Mysqli\\Statement->prepare(  ).../Adapter.php:193';
    
    $result = preg_replace($re, '', $str);
    
    echo $result;
    

    Output

    #TimeMemoryFunctionLocation
    {main}(  ).../http.php:0
    Zend\Mvc\Application->run(  ).../http.php:48
    Zend\EventManager\EventManager->triggerEventUntil(  ).../Application.php:332
    Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
    Zend\Mvc\DispatchListener->onDispatch(  ).../EventManager.php:322
    WebService\Controller\User\ManageAccountController->dispatch(  ).../DispatchListener.php:138
    WebService\Controller\User\ManageAccountController->dispatch(  ).../AbstractRestfulController.php:313
    Zend\EventManager\EventManager->triggerEventUntil(  ).../AbstractController.php:106
    Zend\EventManager\EventManager->triggerListeners(  ).../EventManager.php:179
    WebService\Controller\User\ManageAccountController->onDispatch(  ).../EventManager.php:322
    WebService\Controller\User\ManageAccountController->onDispatch(  ).../AbstractController.php:129
    WebService\Controller\User\ManageAccountController->processPostData(  ).../AbstractRestfulController.php:429
    WebService\Controller\User\ManageAccountController->create(  ).../AbstractController.php:234
    Model\Model\UserModel->updateUser(  ).../ManageAccountController.php:50
    Model\Mapper\UserMapper->updateUserData(  ).../UserModel.php:560
    Application\Database\Connection->execute(  ).../UserMapper.php:475
    Zend\Db\Adapter\Adapter->query(  ).../Connection.php:77
    Zend\Db\Adapter\Driver\Mysqli\Statement->prepare(  ).../Adapter.php:193