Jump to content

Coding Colors


Go to solution Solved by Barand,

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!

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

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.