I am trying to build a Javascript highlighter using PHP for my website, so when user posts <script> something </script> the "something" should get send to my highlighter and somehow combined back into the string and then displayed.

Heres what i have so far, i tried using eregi, but what happened was it would only do it at the first <script></script> ... if there is another <script></script> in the user input it would bug out.
So now im trying to use explode, and im not very good at array sorting, so how would i break the tags apart, then highlight inbetween tags, and then recombine it as a single string? Does anyone have a better function for it? Heres mine:

<?php
// Whats suppose to happen is every thing should be split and all the things inside <script></script>
// should be highlighted.
$dataread = "1 <script> 2 </script> 3 <script> 4 </script> 5";
$exp = explode("</script>", $dataread); // 1 <script> 2             3 <script> 4      5
for($i = 0; $i < count($exp); $i++){
  $exp2 = explode("<script>", $exp[$i]);
  $num = $i + 1;
  $parse->set_lang('javascript');
  $parse->setcode($exp2[$i]);
  echo $parse->parse();
  echo $exp2[$i+2]; // don't know how to recombine.
}
?>

Thats all i got so far. And it obviously won't work, so i am out of ideas.
Any help is appreciated thx.

    Try using preg_replace and a regexp of #<script>(.*?)</script>#is. The ? is the critical bit, and means match as little as possible instead of the default as much as possible. ereg() doesn't have that.

    You could do it with ereg, but you don't wanna see that.

      ok thats cool. i never use preg cuz its more confusing.
      But how would i use that preg expression u gave me into my code?
      i mean ok it saves the stuff inbetween but how would i rearrange it to combine the modified/parsed string with the stuff outside
      i would need something like
      (.)<script>(.)</script>(.*) and then take \2 and parse that SOMEHOW i dunno how... i guess using the preg match and using $arg[1] but then i wouldnt know how to do it forEACH... so i would need a loop right??

      so take \2 and parse that... then recombine it with \1 . <script> . parsed(\2) and combine with . \3 and make it a simple string to display again?
      Can someone create a small function for it? or explain to me how i shouldset up the loops?

        Well, you said you were trying to do with with eregi, but that didn't work; how was that written?

          yea i couldnt highlight multiple tags, and then i wasn't able to recombine it either.

            My Eregi script, was from scratch. Meaning it was highlighting code by itself using arrays with the functions and <span>s.... but the problem with that is comments, for some reason i would have trouble selecting the \n with eregi. And it wouldn't stop highlight in orange on a single-line comment. And also it would sometimes try to highlight the HTML too causing weird characters etc.

            So now im trying to use GeSHi which is a highlighter thats free, but i cant get it to work correctly, its suppose to just highlight <script></script> and send the text in it to the GeSHi class, and then bring it back and recombine it with the script.

            So i would need the preg match
            for

            something <script> something </script> something

            and then combine it back to a string:
            $preg[0].highlight($preg[1]).$preg[2].
            But it would have to be a loop, does anyone know how to make a function for it? I have lots of experience with php, but this little thing is driving me crazy.

              damn, im still trying but still no luck 🙁

                im trying ereg, explode, and preg methods but i cant do it.

                  Oh, all right.

                  
                  function highlight_script($raw_code)
                  {
                     $raw_code = $raw_code[1]; // the function gets passed an array.
                  // Mess around with $raw code however you feel like to get $cooked_code
                    return $cooked_code;
                  }
                  
                  $text_with_highlighted_script = preg_replace_callback('#<script>(.*?)</script>#si',
                          'highlight_script', $text);
                  
                    7 days later

                    Ok sorry i have been busy, and i tested a couple times and couldnt get it to work.
                    you're only highlighting between text tags.

                    How would you recombine it tho?
                    i mean I tried it, and it gave me an error, (maybe ill find it out. but your script prolly works fine)
                    But i was wondering how would i recombine?
                    $something[0].$highlighted_text.$something[2]

                    so i send only the highlighted_text or the stuff in the script tags into the function and recombine it with the rest... It would have to be some sort of loop.

                      There's no point just sending the stuff in the script tags, is there? If you could do that you wouldn't need a regular expression to search for the script tags, would you? $text is whatever you're searching for <script> tags in ($dataread in your original code). What $highlighted_text is and what highlight_script() should do I leave as exercises for the reader (who will know more about the latter question than I do).

                        Ok im sorry, i was being dumb when i wrote that. I had made a small class defining mistake and was not understanding your script correctly. I did it though. And it works great. Thanks for your help, you rock 😃.

                          6 days later
                          Write a Reply...