google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Tô sáng mã nguồn JavaScript và PHP với hàm PHP highlight_string Tô sáng mã nguồn ứng dụng trên nền web là một vấn đề rất được quan tâm của các lập trình viên, nhằm trình bày cho người xem một cái nhìn dễ dàng hơn với mã nguồn của mình. Và vấn đề này cũng đã được hỗ trợ bởi rất tốt bởi nhiều công cụ, dự án JavaScript miễn phí hiện nay.

Tuy nhiên, trong bài viết này, tác giả sẽ hướng dẫn bạn sử dụng hàm highlight_string có sẵn trong ngôn ngữ lập trình PHP để tạo một ứng dụng tô sáng mã nguồn đơn giản. Vui lòng vào bài viết chi tiết để xem thêm.


Nhãn: tô sáng mã nguồn, PHP, highlight_string, lập trình viên, dự án JavaScript

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

PHP has a cool function that automatically highlights PHP code called highlight_string(); Theoretically this could be used to roll your own code highlighting on a site, rather than rely on JavaScript or some kind of external service to do it. In this article I'll show you the basics of how it works, then extended it with a few tricks. Since JavaScript is so similar to PHP in syntax, we can trick the function into highlighting JavaScript code as well. Then finally how we can bust out some smarts to auto-tab the code.

Special thanks to Benjamin Mayo (Darren Beige) who wrote the format_javascript() function we'll check out below.

Basic Usage

The highlight_string() function just accepts a string, which must begin with <?php and end with ?>. by default it echos/prints the line.

<?php highlight_string('<?php
  $i = 1; 

  function rockOut() {
      alert("wah wah wah");
  }
?>'); ?>

The resulting HTML is:

<pre id="code_highlighted"><code><span style="color: #0000BB">
<span style="color: #0000BB">&lt;script type="text/javascript">&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;{
	&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'The&nbsp;value&nbsp;is&nbsp;true'</span><span style="color: #007700">;
	}&nbsp;else&nbsp;{
	&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'The&nbsp;value&nbsp;is&nbsp;false'</span><span style="color: #007700">;
	}


</span><span style="color: #0000BB">&lt;/script&gt;</span>
</span>

Which looks like this:

Pretty wild eh? If you'd rather have that string returned rather than printed, just pass TRUE as a second parameter.

Trick it into highlighting JavaScript

Benjamin Mayo (Darren Beige) put together a PHP function that would trick PHP into highlighting JavaScript code instead of exclusively PHP. Beyond that, it also applies proper tab indentation of code, despite what is present in the file. For example, even if the original code was completely flush left like this, the output will be nicely indented.

Check out the demo to see it in action:

View Demo   Download Files

How it works

The indentation occurs by adding line breaks after every brace and semicolon if they are not already there. This puts each statement on it's own line, priming the code. However, the main bulk of the code for indentation happens in the loop itself.

$lineecho = $line;
if (substr_count($line, "\t") != $tab) {
   $lineecho = str_replace("\t", "", trim($lineecho));
   $lineecho = str_repeat("\t", $tab) . $lineecho;
}
$tab = $tab + substr_count($line, "{") - substr_count($line, "}");

The block works by keeping a count (in the variable $tab) of how many tab characters ("\t") there are on the previous line. The current line is counted for tabs using the substr_count() function. If the two values do not match, the echoed line is padded by the $tab value. This now means that the number of tab characters at the start of the line matches the number in the $tab variable. After this procedure, the new $tab value is calculated by taking the current $tab and adding on the number of opening braces found subtracting the number of closing braces.

The output code is in <pre> tags so the tabs display properly.

Usage

Let's say you wanted to highlight a big chunk of JavaScript code that lived in a file. Easy, just include the PHP file/function, grab the contents of that file, and run the function on it.

<?php
  include_once('format_javascript.php');
  $BigJavaScriptString = file_get_contents('path/to/javascript.js');
  echo format_javascript($testBigJS);
?>

So the deal here is if you want to highlight code this way, you need to get it into a string variable. If you wanted to use this in say, a CMS, you would need to be able to save and run PHP inside the saved content areas. Or, you'd need to write some fancy regex stuff to parse content and look for particular tags and be able to extract the innards into a variable for highlighting. Above my head.

If you are interested in how you might use this to highlight other languages, check out the comment thread in the documentation, which has some attempts at highlighting XML and HTML.

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web