0

i have this string :

Le serveur a retourné une erreur "500 An exception occurred while executing 'select DIVISIONTEST(4,0) from dual': SQLSTATE[HY000]: General error: 20000 OCIStmtExecute: ORA-20000: Bien essayé! ORA-06512: à "FOO.DIVISIONTEST", line 8 (ext\pdo_oci\oci_statement.c:148)".

i would like to show only this part : Bien essayé! nothing before or after this part.

This string is generated by a:

$exception->getMessage()

Can you help me with a regex ? thanks you

EDIT : sorry , the customized message will change. I should have been more precise. I'm looking for a way to get the message after ORA-20000: and before the ORA: following . I'm trying to get exception raised from a PL/SQL request. The error raised is a customized message

4
  • Thanks kevinabelita, you edited my post 5 secs before me Commented May 28, 2014 at 8:39
  • is the word Bien essayé! would change later? Commented May 28, 2014 at 8:42
  • 1
    Pretty hard to come up with a valid Regex with only a single instance of a possible exception string being supplied. Can you give us more examples of what kind of exceptions you need to extract messages from? Commented May 28, 2014 at 8:48
  • Sorry, you are right. I edited my post Commented May 28, 2014 at 8:54

3 Answers 3

3

Well since you only want to match one case, you could use: preg_match
See PHP Manual: https://www.php.net/preg_match

Example:

$subject = $exception->getMessage();
$pattern = '/Bien essayé!/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
Sign up to request clarification or add additional context in comments.

Comments

0

Edit: updated after original post changed:

$re = '/OCIStmtExecute: ORA-[0-9]+: (.+) ORA-[0-9]+:/'; 
$subject = $exception->getMessage();

preg_match($re, $subject, $matches);
print_r($matches);

2 Comments

Ah thanks, i tested it and i've got Warning: preg_match(): Delimiter must not be alphanumeric or backslash edit : it needed backslash. But my array is empty. I keep trying thanks
Sorry, forgot the delimiter. Updated.
-1
<?php 
if (preg_match("/Bien essayé!/i",$exception->getMessage())
   echo : "Bien essayé!";
else
   echo "Pas bien essayé :-D ";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.