I am trying to insert a string of character or insert a word where $string value = Null; using PHP's preg_replace.
I couldn't find a proper regexp which will match null string.
my code is like this:
$string ={member_repeat_designation___designation};
$pattern ='//' ;
$replacement = 'Member';
return preg_replace($pattern, $replacement, $string);
Ok I am submitting the full code here why I need it.
$string1= "Ln. {member___first_name} {member___last_name<br>
{member_repeat_designation___designation}";
$string2="{member_repeat_designation___designation}";
$patterns=array();
$patterns[0]= '/President/';
$patterns[1]= '/Team Leader/';
$replacements=array();
$replacements[0]= '{member_repeat_designation___designation} {member___club_name}';
$replacements[1]= 'TL';
$patt=0;
$rep='Member';
$a=preg_replace($patterns, $replacements, $string1);
$b=preg_replace($patt, $rep, $string2);
return $a <br> $b;
Why do you need regex for this? PHP has an in-built function for this purpose:
$string = '{member_repeat_designation___designation}';
if( is_null($string) ) {
//string is null, do whatever
}
if( trim($string) == '' ) {
//string is empty
}
Documentation: is_null()
If you need to match NULL char,
return preg_replace('/\\0/', $replacement, $string);
However...
Empty value : ""
Null char : "\0"
They are entirely different.