Jump to content

Coding Colors


HunterShutt

Recommended Posts

Hello,

 

I'm Hunter. I'm new to this community and I have a question.

 

So I'm trying to get contents from a text document, which is working fine. But I want to color parts of it like a code editor.

 

So here's my code:

 

<html>
<head>
	<title>Coding</title>
	<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
	<div id="body">
		<h1 class="indent" id="header">Let's Code</h1>
		<div id="textfile">
			<?php
				$file = './test.txt';
				$document = file_get_contents($file);
				//echo $document;

				$lines = explode("\n", $document);
				foreach ($lines as $newlines) {
					echo $newlines.'<br/>';
				}
			?>
		</div>
	</div>
</body>
</html> 

 

 

So I know how I can get the 'parts':

 

$bracket1 = explode('{', $document);
$bracket2 = explode('}', $document);

foreach ($bracket1 as $color1) {
 $color1.(Some color function or method here);
}

foreach ($bracket2 as $color2) {
 $color2.(Some color function or method here);
}

 

 

 

Thanks in advance!

 

EDIT: By the way, if you have an easier idea/way to get the 'parts', please let me know!

Link to comment
https://forums.phpfreaks.com/topic/296614-coding-colors/
Share on other sites

Use styles (CSS)

EG

<?php
$str = "abcd{efgh}ijkl{mnop}qrst{uvwx}yz";
$str = str_replace('{', '{<span>', $str);
$str = str_replace('}', '</span>}', $str);
?>
<html>
<head>
    <style type="text/css">
    div {
        color: #00F;
    }
    span {
        color: #F0F;
    }
    </style>
</head>
<body>
    <h3>The text</h3>
    <div>
        <?=$str?>
    </div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/296614-coding-colors/#findComment-1513075
Share on other sites

Me oh my! 

 

 

 

Use styles (CSS)

EG

<?php
$str = "abcd{efgh}ijkl{mnop}qrst{uvwx}yz";
$str = str_replace('{', '{<span>', $str);
$str = str_replace('}', '</span>}', $str);
?>
<html>
<head>
    <style type="text/css">
    div {
        color: #00F;
    }
    span {
        color: #F0F;
    }
    </style>
</head>
<body>
    <h3>The text</h3>
    <div>
        <?=$str?>
    </div>
</body>
</html>

Brilliant! I've just started learning PHP, I have experience with other languages such as C#, C, C++, Java, JavaScript, and of course HTML and CSS. 

 

I'm assuming str_replace(); just replaces the part of a string. lol

I would've never thought of that, thanks!
 

 

You could also give a shot to highlight_string() or highlight_file(). I've not used either so I can't vouch for their effectiveness, but it's something to look at.

 

Thanks for the feedback!

I'll try it.

Link to comment
https://forums.phpfreaks.com/topic/296614-coding-colors/#findComment-1513092
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.