Soulignant le code source de vos applications sur les pages web est un travail intéressant pour tout programmeur/codeur, pour montrer la plus simple donnent à tous les publics. JavaScript tutorial, L'auteur guide explique comment utiliser highlight_string
fonction, dans le langage de programmation PHP, pour créer un outil simple pour mettre en évidence votre code source de l'application.
- Demo
- Agrandir
- Recharger
- New window
G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte
Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
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"><script type="text/javascript">
</span><span style="color: #007700">if (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {
echo </span><span style="color: #DD0000">'The value is true'</span><span style="color: #007700">;
} else {
echo </span><span style="color: #DD0000">'The value is false'</span><span style="color: #007700">;
}
</span><span style="color: #0000BB"></script></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:
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.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $