Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 preg_replace中的正则表达式不会跨多行替换_Php_Regex_Preg Replace - Fatal编程技术网

Php preg_replace中的正则表达式不会跨多行替换

Php preg_replace中的正则表达式不会跨多行替换,php,regex,preg-replace,Php,Regex,Preg Replace,以下preg_replace检查以*开头的行,并在其周围放置html标记。这工作得很好,唯一的问题是,只有当有一条线路提供给它时,它才会工作,如果有多条线路,它就不会工作 我已经尝试将\s和m添加到它,但没有任何效果。我们可以做些什么来让它工作 $str = <<< EOF *Portfolio Our work includes... *Company Profile Acme was formed in EOF; $str = preg_replace('/^\*([^

以下preg_replace检查以
*
开头的行,并在其周围放置html标记。这工作得很好,唯一的问题是,只有当有一条线路提供给它时,它才会工作,如果有多条线路,它就不会工作

我已经尝试将
\s
m
添加到它,但没有任何效果。我们可以做些什么来让它工作

$str = <<< EOF
*Portfolio
Our work includes...
*Company Profile
Acme was formed in 
EOF;

$str = preg_replace('/^\*([^\*].*)$/', '<h1>$1</h1>', $str);

echo $str;

//Expected output
<h1>Portfolio</h1>
Our work includes...
<h1>Company Profile</h1>
Acme was formed in

//Current output either works if there's only one line.
$str=使用
m
(多行)修饰符执行此操作

$str = preg_replace('/(?m)^\*([^*].*)$/', '<h1>$1</h1>', $str);
$str=preg_replace('/(?m)^\*([^*].*)$/'、'$1',$str);

显示多行的一些示例。如果没有多行标志
/m
^
锚定只在主题字符串的开头匹配,而不是在行的开头匹配。切线:
[^\*]
看起来是错误的,是否打算改为
[^*]
确定。只是出于好奇,正则表达式之间的变化是为了…一个小问题。以多个*开头的行不应匹配。请参阅更新的演示
http://regex101.com/r/lZ1hQ9/3
。这就是为什么那个东西在中间(你改变了):@jmenezes它是固定的。