php Regular expression fails

waghso

I'm trying to match string but it is going into last else loop. Can you please provide any help?

$cstg = "<result>True<\/result>";

if (preg_match("/<result>True<\/result>/", $cstg)) {
    echo "result success";
}
else if (preg_match("/\<result\>False\<\/result\>/", $cstg)) {
    echo "result fail";
}
else {
    echo "Something went wrong.";
}
vanadium23

Issue is coming from your $cstg string. You don't have to escape \ in html text. So following code are working fine:

$cstg = "<result>True</result>";

if( preg_match("/<result>True<\/result>/", $cstg)){
    echo "result success";
}
elseif  (preg_match("/\<result\>False\<\/result\>/", $cstg)){
    echo "result fail";
}else{
    echo "Something went wrong.";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related