[solved] php highlight function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

[solved] php highlight function help

Post by ziggy3000 »

Dont worry i already have my highlight function, and i know how to use it.
This was made by Kai Sellgren

Function.php
edited: i don't feel like revealing this code forever... this problem is solved .
To use this you need to do this in your code

Code: Select all

<?php
include('function.php');
$code = '<?php phpinfo(); ?>';
hc($code,1,1);
?>
The problem is that i want to use this function just by typing

Code: Select all

<code><?php phpinfo(); ?></code>
Can you do this by using php in the css file or what?
Last edited by ziggy3000 on Sat May 12, 2007 11:38 am, edited 4 times in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you are trying to highlight a part of a string that will be enclosed in the <code> element, you can use preg_replace_callback(). If you're asking if a browser can recognize those tags like HTML tags and call a PHP function, it's not possible, CSS or not. PHP code is parsed on the server side, and the resulting output (usually HTML) is returned to the web browser and parsed then by the browser. No PHP code is ever interpreted by the browser.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are going to need to set up something similar in style to a bbCode parser for this. You might have a look at the Wordpress plugin wp-syntax and how the implement the GeSHi highlighter. It is fairly simple as long as you are comfortable with regexp and callbacks.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

Everah, can you give me some links?
i dont use wordpress
and i dont especially use the GeSHi highlighter.(I hate the colors) :x


and aaronhall, can you setup the css file so it interprets php and then you can specify what color the comments, html tags php functions should be.
and then when you include the css file it transforms

Code: Select all

<code>
<?php
//comment
phpinfo();
?>
</code>
to

Code: Select all

<code>
<?php
//comment
phpinfo();
?>
</code>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The closest you can get is to wrap different portions of the to-be-highlighted code in spans with styles attached to them, e.g. <span class="php_comment">//asdfasdf</span>. If might be helpful for you to read an introductory CSS tutorial.

Also, geshi allows you to change the output's style via css.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

is there a really good introductory css crash course tutorial or CSS 101
i could really use it. and i really wouldnt like to write that much code, when i could use the hc function instead.
i was just asking if i could put php in a css file so it automatically converts comments, functions, and stuff without me typeing the html code

For example
on the page i want the higlighted code
i could have it like this

Code: Select all

<code class="php">
<?php
//comment
phpinfo();
?>
</code>
and the css file changes the colors and stuff.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Simply put, no -- CSS doesn't have the facilities to conditionally highlight parts of a class. As for a tutorial, visit your favorite search engine.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

even if you can use php in it?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The HTML and CSS is what a PHP script would return or output to the browser... the browser then interprets that HTML/CSS to render the page. PHP is completely finished processing the file by the time the browser starts interpreting the html/css. Additionally, CSS doesn't have the same facilities PHP does to operate on strings. Again, have a look at an introductory CSS tutorial to get a better scope of the language.

I should have asked this before, but why are you trying to do this? There's probably a better (feasible) approach.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

l o l i know how php works!!
i wanted to do this so my html code would look like

Code: Select all

<html>
//link to css file
<code type="php">//php code here</code>
</html>
and the css file uses the php function so it replaces

Code: Select all

//php code here
with a random 5 digit variable(in case i use it again on that page, if so i need it to be random)
and uses that same variable in the function. so it would replace the above code like so

Code: Select all

<html>
//link to css file
<code type="php">
<?php
$fivedigitvar = "//php code here";
hc($fivedigitvar,1,1);
?>
</code>
</html>
I want to do this so i wouldn't have to type as much in the code for my website
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Again, no, css has no facilities to manipulate strings on the fly, with or without PHP code in the CSS file.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok :cry:
thanks for trying...
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

what if i want to create bbcode, so when i parse this

Code: Select all

<code><?php
//comment
 ?></code>
the parser will make it look like the way it does here
Last edited by ziggy3000 on Sat Apr 07, 2007 9:37 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's possible to do.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i want to use this code
but i dont know how to use it. i asked there too(at the bottom), but no one's replied yet.
how should i call the function

or is there a better bbcode script? i am looking on google, and i cant find any "good" scripts
Post Reply