foevah Posted June 4, 2008 Share Posted June 4, 2008 Hi everyone, I would like to know how to output a .txt into a PHP page and then style the text that is inside the .txt file. For example lets say I have this in the .txt file: Hello my name is Mattew Then on my PHP page I would like the .txt file to be shown with styles like this: Hello my name is Mathew Please can someone help me? I have done some research but I have not had much luck in finding an exact solution. Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/ Share on other sites More sharing options...
The Little Guy Posted June 4, 2008 Share Posted June 4, 2008 I would use regular expressions to do this, and make bbcode or something... http://phpsnips.com/snippet.php?id=41 Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557516 Share on other sites More sharing options...
nafetski Posted June 4, 2008 Share Posted June 4, 2008 In that particular example you could also explode the string, and style the first and last element. If it always works that way =) Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557661 Share on other sites More sharing options...
Jabop Posted June 4, 2008 Share Posted June 4, 2008 Sounds like you're going to have to use a lot of preg_replace(); Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557667 Share on other sites More sharing options...
discomatt Posted June 4, 2008 Share Posted June 4, 2008 Did you just want to style random words with random styles or what? Throw us a bone, please Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557671 Share on other sites More sharing options...
thebadbad Posted June 4, 2008 Share Posted June 4, 2008 If you for example want all instances of "Mathew" to be in red, str_replace (or str_ireplace, for case insensitivity) will suffice: <?php $text = file_get_contents('path/to/filename.txt'); // $text now holds 'Hello my name is Mathew' $text = str_replace('Mathew', '<span style="color: red;">Mathew</span>', $text); echo $text; // Hello my name is <span style="color: red;">Mathew</span> ?> Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557672 Share on other sites More sharing options...
The Little Guy Posted June 4, 2008 Share Posted June 4, 2008 If you plan you use BBC, then I just want you to know that I updated the code: http://phpsnips.com/snippet.php?id=41 Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557686 Share on other sites More sharing options...
thebadbad Posted June 4, 2008 Share Posted June 4, 2008 If you plan you use BBC, then I just want you to know that I updated the code: http://phpsnips.com/snippet.php?id=41 If you haven't already found it; you've made a mistake - remove 'px' from the "color" replacement Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557694 Share on other sites More sharing options...
The Little Guy Posted June 4, 2008 Share Posted June 4, 2008 If you plan you use BBC, then I just want you to know that I updated the code: http://phpsnips.com/snippet.php?id=41 If you haven't already found it; you've made a mistake - remove 'px' from the "color" replacement Oh, thank you, its fixed. Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557703 Share on other sites More sharing options...
discomatt Posted June 4, 2008 Share Posted June 4, 2008 Or even better http://php.net/manual/en/intro.bbcode.php Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557706 Share on other sites More sharing options...
thebadbad Posted June 4, 2008 Share Posted June 4, 2008 The Little Guy - you forgot to escape the square brackets in the patterns, and I would add the s modifier to match newline characters also. Quote Link to comment https://forums.phpfreaks.com/topic/108719-styling-a-txt-file/#findComment-557715 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.