Question

<?php     
$value='A';
    if(preg_match("/|/",$value)){
        echo 'This is no good!';
    }
    else{
        echo 'Works';
    }
?>

If you execute the above code you can see that the preg_match() is not working while I don't have | or pipeline in the variable.

Why preg_match is not working for single character or pipeline character?

Was it helpful?

Solution

The vertical pipe character (|) has a special meaning when used inside a regular expression. It is used for specifying alternatives in a regular expression. You need to properly escape it if you want to match the literal | character:

if(preg_match("/\|/",$value)){
    # code ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top