|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Code for forums
i created a simple forums and i want to implament a php code like we have here with the [php] etc tags...any suggestions...or a place where i can find it?
|
|
#2
|
|||
|
|||
|
I'm pretty sure theres a function to syntax highlight php code which shouldn't be too hard to find on php.net then you can just do a strpos for the opening and closing tags and run the contents through that.
Hope that helps, -KM- |
|
#3
|
|||
|
|||
|
tried lookin and also applying what i found but no luck...i just want it to display a certain code like it is done in this forum
|
|
#4
|
|||
|
|||
|
Ok then let me clarify a bit. Assuming for now that you're not parsing the string for anything else (which you're bound to be so you'll have to adapt this advice appropriately) the first task is to chop the posts up into three sections.
1 - The text before the php tag 2 - The text between the php and /php tags 3 - The text after the /php tag The key function here being strpos int strpos ( string haystack, string needle [, int offset]) So by making the haystack $post_text and the needle 'php' we can find out where that opening tag begins. Similarly by keeping the haystack the same and making '/php' the needle we can find where the close tag begins. The next function you'll want is substr. string substr ( string string, int start [, int length]) You should be able to feed the post text and offsets strpos returned for you into here to break the post up into the three parts. Having done that you can echo the first section to the screen, run the middle part through highlight_string which will syntax highlight it for you then echo the last part to the screen. One quick note on highlight_string, it takes two arguments. The first is the string to be highlighted the second is a boolean value. If you set the second to true then it will return the highlighted string to you if not it echos it to the screen. NB: If you want multiple sections of code in posts then you'll either want a while loop that keeps going until the strpos searching for 'php' returns false or some kind of regular expression. Hope that helps, -KM- Further note: Throughout this post I've removed the square brackets surrounding php and /php to avoid this forum treating them as the start and end of code blocks. When you start coding don't forget to add them back in. Last edited by kode_monkey : October 8th, 2003 at 02:47 AM. |
|
#5
|
|||
|
|||
|
lost me after this string substr ( string string, int start [, int length])
...=[ ... i get the rest except the substr thing...thx though...will this be under one function? or more than one? Last edited by Bruski : October 8th, 2003 at 11:11 AM. |
|
#6
|
|||
|
|||
|
i tried making this code but it doesnt color code and it doesnt display most of it =[
PHP Code:
|
|
#7
|
|||
|
|||
|
Sorry Bruski I'm not a regular expressions guy (ask anyone on my comp sci course and they'll tell you I frequently deny that they exist)
Using strpos and substr will work perfectly well for this but if you want to go down the regular expressions route then I'm afraid I'll have to pass you over to someone else. Will keep monitoring this thread, if you have any more questions about the approach I suggested feel free to post them and I'll be happy to help, -KM- |
|
#8
|
|||
|
|||
|
how would i use substr to split the text ... i get the strpos
PHP Code:
im confused aout the substr because on php.net it talks about using integers to extract text... thx for, really appreciate it ![]() php should have brakets |
|
#9
|
|||
|
|||
|
strpos returns the location of a given string within another string.
For example, if we're looking for the string 'needle' in the string 'hay needle stack' then we can use - PHP Code:
In this case $offset will be set to 4 since thats where the string 'needle' starts in the other string. If we give this offset to substr then we can extract all text from that point to the end of the string. PHP Code:
Having done this $sub_string will be set to 'needle stack'. In your case you want to use strpos to find the offsets of 'php' and '/php' (remembering to add square brackets) then substr to extract the parts of the string and parse them appropriately. If you're still unsure I recommend playing about with strpos and substr using different strings and arguments values. When you see them working its all far more obvious than in one off examples like this. Hope this helps, -KM- |
|
#10
|
|||
|
|||
|
PHP Code:
ran that to see if SELECT would highlight but...it doesnt |
|
#11
|
|||
|
|||
|
Ok here we go, I finally got around to just writing this for you rather than trying to explain. Hopefully this working example will push you in the right direction towards what you need.
PHP Code:
Although this works fine there are some problems with it that you'll need to adress but based on this you should be able to figure out how to do for yourself. 1 - The highlight_string function will NOT highlight unless the <? and ?> tags are present. 2 - It'll only work on the first section of code the rest will remain unhighlighted so you'll have to add a while loop in there to highlight all of them. Hope this helps, -KM- NB: As always you'll need to add square brackets around each 'php' for this to work. |
|
#12
|
|||
|
|||
|
thx =]
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Code for forums |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|