Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP上用正则表达式替换文本?_Php_Regex - Fatal编程技术网

如何在PHP上用正则表达式替换文本?

如何在PHP上用正则表达式替换文本?,php,regex,Php,Regex,我有一个类似“MASTER_teaching.NAME”的文本,所以想用空格或其他字符串替换*MASTER_teaching*。如何在regexphp上实现这一点 *我对REGEX是个新手。谢谢如果只替换字符串而不是模式,可以使用str_replace使用正则表达式或简单字符串函数 $string = 'MASTER_LECTURE.NAME'; $string_replace = 'MASTER_LECTURE'; // Regular expression preg_replace('/

我有一个类似“MASTER_teaching.NAME”的文本,所以想用空格或其他字符串替换*MASTER_teaching*。如何在regexphp上实现这一点


*我对REGEX是个新手。谢谢

如果只替换字符串而不是模式,可以使用str_replace

使用正则表达式或简单字符串函数

$string = 'MASTER_LECTURE.NAME';

$string_replace = 'MASTER_LECTURE';

// Regular expression
preg_replace('/'.preg_quote($string_replace).'/is', '', $string);

// String function
$string = str_replace($string_replace, '', $string);


您不需要公式。它只是str_replace($search,$replace,$subject)。如果它真的是一个单词而不是一种模式,你应该使用它,因为它很轻。我需要这个模式,因为单词将由dinamic.nice@delphist生成。你能告诉我什么时候word MASTER_讲座将取决于我们提交的字符串吗?