简体   繁体   English

如何自动将粘贴的代码中的选项卡(从.net,netbeans,notepad,sublime)转换为nbsp; 在PHP中

[英]How to automatically convert the tabs in the pasted code(from .net, netbeans, notepad,sublime) to nbsp; in php

So I have this blog which I wrote in php where I post articles regarding programming and embedded systems, its not so famous but I am doing this in a hope that it will serve as a leverage in interviews, I am in final year of college. 因此,我有一个用php写的博客,我在博客上发表有关编程和嵌入式系统的文章,虽然它并不那么著名,但是我这样做是希望它可以在面试中发挥作用,我在大学的最后一年。

I used to contribute to mediawiki and I got inspired by the wikilanguage so I built myself a wikilanguage look-a-like 我曾经为MediaWiki做出过贡献,并受到Wiki语言的启发,所以我建立了自己的Wiki语言外观

And in this language when ever I had code to paste in my blog I would surround the code with a tag 用这种语言,当我将代码粘贴到博客中时,我会在代码周围加上标签

I code in a variety of languages hence I end up using a variety of IDE's I have used netbeans, .net studio, notepad, sublime, gedit etc.. 我使用多种语言编写代码,因此最终使用了多种IDE,例如netbeans,.net studio,notepad,sublime,gedit等。

private     function makecode($matches){
                $i=0;
                $out = '</br></br><div id = "cod"><table border = 0 width = 600px cellspacing = 0px> <tr></tr>';
                $matcha = htmlspecialchars(($matches[1]));
                                preg_replace("/\t/", "&nbsp;&nbsp;&nbsp;&nbsp;", $matcha);
                $lines = explode("\n", $matcha);

                foreach($lines as $line){
                    if(preg_match('/^\s*#/',$line)){
                        $out .= "<tr><td><font color = \"grey\"><i><small>$line</small></i></font></td></tr>";
                        continue;
                    }
                    if(preg_match('/^\s*$/',$line))continue;
                    $i++;
                    $out .= "<tr><td><small>$i</small>. $line</td></tr>";
            }
                $out .= "</table></div>";

                return $out;
        }

private function coder(){
        $this->text = preg_replace_callback('/<c>(.*?)<cc>/s', array($this,'makecode'),$this->text);

            }

Here's my code for manipulating the code tag. 这是我用于操作代码标签的代码。

right now am trying pasting code from notepad and I am replacing the \\t with 4 nbsp; 现在正在尝试从记事本粘贴代码,我用4 nbsp替换了\\ t; assuming that most of the ide's represent there tabs with a '\\t' could anyone suggest me a more elegant way of handling the tabs. 假设大多数ide都在其中以“ \\ t”表示选项卡,那么有人可以建议我以更优雅的方式处理选项卡。 I do not want to use a ready made library for this. 我不想为此使用现成的库。

What about using this ? 那怎么用呢?

private function makecode($matches) {
    return '<pre>'.htmlspecialchars($matches[1]).'</pre>';
}

And you can set the tab size with css : 您可以使用CSS设置标签大小:

pre {
    -moz-tab-size:    4;
    -o-tab-size:      4;
    -webkit-tab-size: 4;
    -ms-tab-size:     4;
    tab-size:         4;
}

EDIT : My mistake, I haven't seen that you need lines numbers ... 编辑:我的错误,我还没有看到您需要行号...

try to use Eclipse or PHPStorm(my recommendations) - they can format your code for you(also you can setup your own style of coding) 尝试使用Eclipse或PHPStorm(我的建议)-它们可以为您格式化代码(也可以设置自己的编码样式)

http://www.eclipse.org/pdt/updates/ http://www.eclipse.org/pdt/updates/

http://www.jetbrains.com/phpstorm/ http://www.jetbrains.com/phpstorm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM