PHP preg_replace在null字符串上

时间:2021-06-16 08:45:22

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.

我试图插入一个字符串或插入一个单词$ string value = Null;使用PHP的preg_replace。我找不到匹配空字符串的正确正则表达式。

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;

2 个解决方案

#1


2  

Why do you need regex for this? PHP has an in-built function for this purpose:

为什么你需要正则表达式呢? PHP具有用于此目的的内置函数:

$string = '{member_repeat_designation___designation}';
if( is_null($string) ) {

//string is null, do whatever

}
if( trim($string) == ''  ) {

//string is empty

}

Documentation: is_null()

文档:is_null()

#2


1  

If you need to match NULL char,

如果你需要匹配NULL char,

return preg_replace('/\\0/', $replacement, $string);

However...

然而...

Empty value : ""
Null char   : "\0"

They are entirely different.

它们完全不同。

#1


2  

Why do you need regex for this? PHP has an in-built function for this purpose:

为什么你需要正则表达式呢? PHP具有用于此目的的内置函数:

$string = '{member_repeat_designation___designation}';
if( is_null($string) ) {

//string is null, do whatever

}
if( trim($string) == ''  ) {

//string is empty

}

Documentation: is_null()

文档:is_null()

#2


1  

If you need to match NULL char,

如果你需要匹配NULL char,

return preg_replace('/\\0/', $replacement, $string);

However...

然而...

Empty value : ""
Null char   : "\0"

They are entirely different.

它们完全不同。