HunterShutt Posted June 3, 2015 Share Posted June 3, 2015 (edited) 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 June 3, 2015 by HunterShutt Quote Link to comment https://forums.phpfreaks.com/topic/296614-coding-colors/ Share on other sites More sharing options...
Solution Barand Posted June 3, 2015 Solution Share Posted June 3, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/296614-coding-colors/#findComment-1513075 Share on other sites More sharing options...
maxxd Posted June 3, 2015 Share Posted June 3, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296614-coding-colors/#findComment-1513089 Share on other sites More sharing options...
HunterShutt Posted June 3, 2015 Author Share Posted June 3, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296614-coding-colors/#findComment-1513092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.