Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php],[js] - Jak napisać to w PHP ?
Forum PHP.pl > Forum > Przedszkole
Setesh
Witam.

Znalazłem w pewnym forum kawałek kodu w JS który bardzo by mi się przydał dla mojej stronki.

kod wygląda tak :
  1. //----------------------------------------------------
  2. // Prosty tag ([tag]tekst[/tag])
  3. //----------------------------------------------------
  4.  
  5. function simpletag(thetag) {
  6. tag("[" + thetag + "]", "[/" + thetag + "]");
  7. }
  8.  
  9. //----------------------------------------------------
  10. // Taguje tekst
  11. //----------------------------------------------------
  12.  
  13. function tag(bbopen, bbclose) {
  14. var txtarea = document.form.text;
  15. if ((clientVer >= 4) && is_ie && is_win) {
  16. //Win IE
  17. theSelection = document.selection.createRange().text;
  18. if (!theSelection) {
  19. txtarea.value += bbopen + bbclose;
  20. txtarea.focus();
  21. return;
  22. }
  23. document.selection.createRange().text = bbopen + theSelection + bbclose;
  24. txtarea.focus();
  25. return;
  26. } else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
  27. //Mozilla/gecko
  28. mozTag(txtarea, bbopen, bbclose);
  29. return;
  30. } else {
  31. //inna
  32. txtarea.value += bbopen + bbclose;
  33. txtarea.focus();
  34. }
  35. storeCaret(txtarea);
  36. }
  37.  
  38. //----------------------------------------------------
  39. // Taguje tekst pod mozilla
  40. //----------------------------------------------------
  41.  
  42. function mozTag(txtarea, open, close) {
  43. var selLength = txtarea.textLength;
  44. var selStart = txtarea.selectionStart;
  45. var selEnd = txtarea.selectionEnd;
  46. if (selEnd == 1 || selEnd == 2) {
  47. selEnd = selLength;
  48. }
  49.  
  50. var s1 = (txtarea.value).substring(0,selStart);
  51. var s2 = (txtarea.value).substring(selStart, selEnd);
  52. var s3 = (txtarea.value).substring(selEnd, selLength);
  53. txtarea.value = s1 + open + s2 + close + s3;
  54. return;
  55. }


funkcja ta zamienia tekst typu [ quote ] Coś [ / quote ]
Cytat
Jakiś tekst
i odpowiednio coś z nim robi według tego jak ustalimy.

Chciałem to napisać w PHP aby mi automatycznie zmieniało wiele rzeczy (coś jak na forum dodawanie kodu PHP czyli w znacznikach). Problem w tym że nie mam pojęcia jak to zrobić sad.gif

Wie ktoś z was jak to napisać lub gdzie jest przykład jakiś tego ?
tommy4
Nie jest to skrypt najwyższych lotów, ale mi działa. smile.gif

Coś się psuje z tym forum, także masz linka: http://tommy4.gtacartel.com/bbcode.txt
Setesh
No ładny skrypcik ale... mi chodzi o kolorowanie składni języka c++ - coś jak gdy wstawiamy kod w php tu na forum - dajemy tylko [ php ] KOD PHP [ / php ] a skrypt sam już generuje kod php ze zmianą koloru itp.

Ja chciałem taką funkcję napisać co właśnie będzie tylko tekst między tymi znacznikami konwertować a reszte zostawi w spokoju - tak jak tu na forum - ale nie daje sobie z tym rady sad.gif
tommy4
jesli sie nie myle, to tu na forum pewnie korzystaja z kolorowania skladni wykonanego przez tworcow php.

http://pl2.php.net/manual/en/function.highlight-string.php
Kicok
Cytat
Ja chciałem taką funkcję napisać co właśnie będzie tylko tekst między tymi znacznikami konwertować a reszte zostawi w spokoju - tak jak tu na forum - ale nie daje sobie z tym rady


Użyj preg_replace_callback" title="Zobacz w manualu PHP" target="_manual, wyrażenie do pobrania zawartości BBcode:
  1. <?php
  2. $pattern = '/[nazwa_tagu](.*?)[/nazwa_tagu]/si';
  3. ?>
Setesh
Ok... wykombinowałem coś takiego :

  1. <?php
  2. //-----------------------------------------------------//-----------------------------------------------------//----------------------------------------------------- 
  3.  
  4.  function WinToISO($NapisDoZmiany)
  5.  {
  6. $Win = array(
  7.  'Ą',
  8.  'ą',
  9.  'Ś',
  10.  'ś',
  11.  'Ź',
  12.  'ź' 
  13. );
  14. $ISO = array(
  15.  'ˇ',
  16.  'ą',
  17.  'Ś',
  18.  'ś',
  19.  'Ź',
  20.  'Ľ'
  21. );
  22. return trim(str_replace($Win, $ISO, $NapisDoZmiany));
  23.  };  
  24.  
  25. //-----------------------------------------------------//-----------------------------------------------------//-----------------------------------------------------
  26.  
  27.  function WinToCPP($NapisDoZmiany)
  28.  {
  29. $Win = array( 
  30.  ' ',
  31.  '  ',
  32.  'private',
  33.  'public',
  34.  'asm',
  35.  '&nbsp;try',
  36.  ' try',
  37.  'catch',
  38.  '__finally',
  39.  'classid',
  40.  'for(',
  41.  'const ',
  42.  ' if',
  43.  '&nbsp;if',
  44.  'else',
  45.  ' int ',
  46.  '(int)',
  47.  'bool ',
  48.  'char',
  49.  'float ',
  50.  'sizeof',
  51.  'struct',
  52.  'true',
  53.  'false',
  54.  'switch(',
  55.  'case',
  56.  'break;',
  57.  'void ',
  58.  'new ',
  59.  'this',
  60.  'delete',
  61. '__fastcall',
  62.  'UWAGA',
  63.  'Unit1.cpp',
  64.  'Unit1.h',
  65.  'Project1.cpp',
  66.  'return',
  67.  '//<--',
  68.  '-line-'
  69. );
  70. $CPP = array(
  71. '&nbsp;&nbsp;',
  72.  '&nbsp;&nbsp;&nbsp;',
  73.  '<b>private</b>',
  74.  '<b>public</b>',
  75.  '<b>asm</b>',
  76.  '<b>&nbsp;try</b>',
  77.  '<b>&nbsp;try</b>',
  78.  '<b>catch</b>',
  79.  '<b>__finally</b>',
  80.  '<b>classid</b>',
  81.  '<b>for</b>(',
  82.  '<b>const </b>',
  83.  '<b> if</b>',
  84.  '<b>&nbsp;if</b>',
  85.  '<b>else</b>',
  86.  '<b> int </b>',
  87.  '(<b>int</b>)',
  88.  '<b>bool </b>',
  89.  '<b>char</b>',
  90.  '<b>float&nbsp;</b>',
  91.  '<b>sizeof</b>',
  92.  '<b>struct</b>',
  93.  '<b>true</b>',
  94.  '<b>false</b>',
  95.  '<b>switch</b>(',
  96.  '<b>case </b>',
  97.  '<b>break</b>;',
  98.  '<b>void </b>',
  99.  '<b>new </b>',
  100.  '<b>this</b>',
  101.  '<b>delete</b>',
  102.  '<b>__fastcall</b>',
  103.  '<font color=red><b>UWAGA</b></font>',
  104.  '<font color=green><b>Unit1.cpp</b></font>',
  105.  '<font color=red><b>Unit1.h</b></font>',
  106.  '<font color=blue><b>Project1.cpp</b></font>',
  107.  '<b>return</b>',
  108.  '<font color="#000080">//<--</font>',
  109.  '<font color="#000080">//---------------------------------------------------------------------------</font>'
  110. );
  111. return trim(str_replace($Win, $CPP, $NapisDoZmiany));
  112.  }
  113.  
  114. //-----------------------------------------------------//-----------------------------------------------------//-----------------------------------------------------
  115.  
  116.  function IncludeME($NapisDoZmiany, $Znak)
  117.  {
  118. if($Znak == 1) 
  119. {
  120.  return '<font color="#008000">#include "'.$NapisDoZmiany.'"</font>';
  121. }else{
  122. return '<font color="#FF0000">#include &lt;'.$NapisDoZmiany.'&gt;</font>';
  123.  }; 
  124.  }
  125.  
  126. //-----------------------------------------------------//-----------------------------------------------------//-----------------------------------------------------
  127.  
  128.  function bbcode($s)
  129.  {
  130. $one = str_replace("]n", "]", $s);
  131. $match_one = array('##include <(.*?)>#se');
  132. $replace_one = array("IncludeME(stripslashes('$1'), 0)");
  133. $body_one = preg_replace($match_one, $replace_one, $one);
  134.  
  135. $two = str_replace("]n", "]", $body_one);
  136. $match_two = array('##include "(.*?)"#se');
  137. $replace_two = array("IncludeME(stripslashes('$1'), 1)");
  138. $body_two = preg_replace($match_two, $replace_two, $two);
  139.  
  140. $three = str_replace("]n", "]", $body_two);
  141. $match_three = array('#
    1. (.*?)
    #se'
    );
  142. $replace_three = array("WinToCPP(stripslashes('$1'))");
  143. return preg_replace($match_three, $replace_three, $three);
  144. }
  145. //-----------------------------------------------------//-----------------------------------------------------//-----------------------------------------------------
  146.  
  147. $tekst = 'Aby po kliknięciu prawym przyciskiem myszy na element w TListBox zaznaczał się o
    n tak samo jak przy kliknięciu lewym przyciskiem myszy należy napisać funkcje obs
    ługi zdarzeń OnMouseDown i OnMouseUp dla tego TListBox:
  148.  
    1. Unit1.cpp</span></div></li><li class="li2"><div class="de2"><span class="st0">#include "tapi.h"</span></div></li><li class="li1"><div class="de1"><span class="st0">#include "mapi.hpp"</span></div></li><li class="li2"><div class="de2"><span class="st0">#include "zapi.h"</span></div></li><li class="li1"><div class="de1"><span class="st0">#include "kapi.hpp"</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"><span class="st0">#include <tapi.h></span></div></li><li class="li2"><div class="de2"><span class="st0">#include <mapi.hpp></span></div></li><li class="li1"><div class="de1"><span class="st0">#include <zapi.h></span></div></li><li class="li2"><div class="de2"><span class="st0">#include <kapi.hpp></span></div></li><li class="li1"><div class="de1"><span class="st0">-line-</span></div></li><li class="li2"><div class="de2"><span class="st0">void __fastcall TForm1::ListBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)</span></div></li><li class="li1"><div class="de1"><span class="st0">{</span></div></li><li class="li2"><div class="de2"><span class="st0"> Label1->Caption = "Down";</span></div></li><li class="li1"><div class="de1"><span class="st0"> if (Button == mbRight)</span></div></li><li class="li2"><div class="de2"><span class="st0"> {</span></div></li><li class="li1"><div class="de1"><span class="st0"> X = (X + ListBox1->Left + Left << 16) / Screen->Width;</span></div></li><li class="li2"><div class="de2"><span class="st0"> Y = (Y + ListBox1->Top + Top << 16) / Screen->Height;</span></div></li><li class="li1"><div class="de1"><span class="st0"> ::mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);</span></div></li><li class="li2"><div class="de2"><span class="st0"> }</span></div></li><li class="li1"><div class="de1"><span class="st0">}</span></div></li><li class="li2"><div class="de2"><span class="st0">-line-
  149.  
  150. Unit1.cpp  A teraz sprawdzamy jak to wyjdzie ;-) if bool
  151.  
    1. Unit1.cpp</span></div></li><li class="li1"><div class="de1"><span class="st0">-line-</span></div></li><li class="li2"><div class="de2"><span class="st0">void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)</span></div></li><li class="li1"><div class="de1"><span class="st0">{</span></div></li><li class="li2"><div class="de2"><span class="st0"> Label1->Caption = "UP";</span></div></li><li class="li1"><div class="de1"><span class="st0"> if (Button == mbRight)</span></div></li><li class="li2"><div class="de2"><span class="st0"> {</span></div></li><li class="li1"><div class="de1"><span class="st0">= (+ ListBox1->Left + Left << 16) / Screen->Width;</span></div></li><li class="li2"><div class="de2"><span class="st0">= (+ ListBox1->Top + Top << 16) / Screen->Height;</span></div></li><li class="li1"><div class="de1"><span class="st0"> ::mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);</span></div></li><li class="li2"><div class="de2"><span class="st0"> }</span></div></li><li class="li1"><div class="de1"><span class="st0">}</span></div></li><li class="li2"><div class="de2"><span class="st0">-line-
    '
    ;
  152.  
  153. //-----------------------------------------------------//-----------------------------------------------------//----------------------------------------------------- 
  154.  
  155.  $tekst = nl2br(WinToISO($tekst));
  156.  echo bbcode($tekst);
  157.  
  158. //-----------------------------------------------------//-----------------------------------------------------//----------------------------------------------------- 
  159.  
  160. ?>


I... wszystko fajnie ale... chciałem jeszcze dodać do tego aby wsystkie stringi mi kolorował na niebiesko i... wszytko fajnie bo można zrobić to tak :

  1. <?php
  2. $str = '
  3. #include "bibl.h"<br> // TO MIALO BYC NIE RUSZONE :(
  4. #include "bibl.hpp"<br> // TO TEŻ :(
  5. #include <bibl.h><br>
  6. #include <bibl.hpp><br><br><br>
  7. Label1->Caption = "Button 1";'; // TO MIALO ZMIENIC TYLKO MIĘDZY ZNAKAMI (") :(
  8.  
  9. // $str = preg_replace( '/"(|(.*?[^]))"/', '<font color="green">"$1"</font>', $str );
  10. $str = preg_replace( '/"(|(.*?[^]))"/', '<font color="blue">"$1"</font>', $str );
  11. echo( $str );
  12. ?>


ale... wtedy mi zrobi z #include sieczkę sad.gif

Jak to zrobić aby nie ruszało mi tekstu z #include nawet kiedy jest między (") ?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.