Php 用制表符替换空格缩进
我期待着取代4个空格在一行开始的标签,但没有进一步的文本时存在 为了可读性,我的Php 用制表符替换空格缩进,php,regex,Php,Regex,我期待着取代4个空格在一行开始的标签,但没有进一步的文本时存在 为了可读性,我的/{4}+/或/[]{4}+/的初始正则表达式显然起了作用,但显然任何带有四个空格的实例都将被替换 $string = ' this is some text --> <-- are these tabs or spaces?'; $string .= "\n and this is another line singly indented"; // I wro
/{4}+/
或/[]{4}+/
的初始正则表达式显然起了作用,但显然任何带有四个空格的实例都将被替换
$string = ' this is some text --> <-- are these tabs or spaces?';
$string .= "\n and this is another line singly indented";
// I wrote 4 spaces, a tab, then 4 spaces here but unfortunately it will not display
$string .= "\n \t and this is third line with tabs and spaces";
$pattern = '/[ ]{4}+/';
$replace = "\t";
$new_str = preg_replace( $pattern , $replace , $string );
echo '<pre>'. $new_str .'</pre>';
如果您不是正则表达式专家,这可能对您最有意义,并且更容易适应类似的用例(这不是最有效的代码,但它是最“可读”的imho):
//用应用的结果替换所有正则表达式匹配项
//$matches数组的给定匿名函数
函数tabs2spaces($s_带_空格){
//在执行任何其他操作之前,请将现有选项卡替换为4个空格
//允许同质翻译
$s_with_spaces=str_replace(“\t”,”,$s_with_spaces);
返回preg_replace_回调(
“/^(+[]+)/m”,
功能($ms){
//$ms[0]-完全匹配
//$ms[1]-是正则表达式的第一个(…)组
//…在这里,您可以添加额外的逻辑来处理
//前导空格不是4的倍数
返回stru重复(“\t”,楼层(strlen($ms[1])/4));
},
$s_,带_空格
);
}
//示例(使用点使空格可见以进行解释)
$s_with_spaces=我会这样做
请彻底重新评估代码,因为我已经以一种快速直观的方式完成了这项工作
因为我无法运行PHP服务器来测试它:(但应该可以帮助您解决此问题。我可能遗漏了一些东西。您的问题只是关于用一个选项卡替换四个空格,还是还有其他问题?请尝试pregôreplace('~(?:^ | \G)[{4}~m',“\t',$s),参见.WKTROSTRIBI.EW,这仍然没有在第三行中用一个选项卡在中间,这是当前输入和预期输出。HMM,我写的第三行是4个空格,一个制表符和4个空格,但是这似乎没有出现在堆栈溢出问题中。尾随空格保留谢谢,不幸的是,这在第三行(显示2个制表符,然后是4个空格)中也有不足之处-尽管如此,我还是更喜欢使用正则表达式,这样它就可以应用到其他语言中。@Lucas在进行转换之前,您可以简单地用空格替换所有现有的制表符,使其在第三行也能工作(如果您还想避免后面的制表符,也可以将此转换细化为基于bee正则表达式的转换)。我更新了解决方案,至少在最简单的情况下解决了这个问题。我建议不要使用“monter正则表达式”:每当我在代码审阅中发现它们时,我都会要求使用更多代码(和注释)进行重写+simplere regex,或者在可能的情况下根本不使用regeze,或者在最坏的情况下,从较小的字符串(字符串之间有足够的注释)组装一个较大的regex-代码可读性最重要:)
First line has 8 spaces at the start, some text with 4 spaces in the middle.
I am looking for 2 tabs at the start and no change to spaces in the text.
Second line has 4 spaces at the start.
I am looking to have only 1 tab at the start of the line.
Third line has 4 spaces, 1 tab and 4 spaces.
I am looking to have 3 tabs at the start of the line.
// replace all regex matches with the result of applying
// a given anonymous function to a $matches array
function tabs2spaces($s_with_spaces) {
// before anything else, replace existing tabs with 4 spaces
// to permit homogenous translation
$s_with_spaces = str_replace("\t", ' ', $s_with_spaces);
return preg_replace_callback(
'/^([ ]+)/m',
function ($ms) {
// $ms[0] - is full match
// $ms[1] - is first (...) group fron regex
// ...here you can add extra logic to handle
// leading spaces not multiple of 4
return str_repeat("\t", floor(strlen($ms[1]) / 4));
},
$s_with_spaces
);
}
// example (using dots to make spaces visible for explaining)
$s_with_spaces = <<<EOS
no indent
....4 spaces indent
........8 spaces indent
EOS;
$s_with_spaces = str_replace('.', ' ');
$s_with_tabs = tabs2spaces($s_with_spaces);
$str = "...";
$pattern = "'/^[ ]{4}+/'";
$replace = "\t";
$multiStr = explode("\n", $str);
$out = "";
foreach ($multiStr as &$line) {
$line = str_replace("\t", " ",$line);
$out .= preg_replace( $pattern , $replace , $line )
}
$results = implode("\n", $out);