Autor | Zpráva | ||
---|---|---|---|
jerremy Profil * |
#1 · Zasláno: 1. 4. 2012, 13:46:25
Mám jednoduchý skript na formatovanie <textarea> viz kod:
<?php function makeFormattedText($text) { $text = str_replace("[BR]",'<br>',$text); $text = str_replace("",'<strong>',$text); $text = str_replace("",'</strong>',$text); $text = str_replace("[U]",'<span style="text-decoration:underline;">',$text); $text = str_replace("[/U]",'</span>',$text); $text = str_replace("",'<em>',$text); $text = str_replace("",'</em>',$text); $text = str_replace("[A]","<a href=\"\">",$text); $text = str_replace("[/A]","</a>",$text); $text = str_replace("\n","<br>",$text); $text = StripSlashes($text); return($text); } ?> <script type="text/javascript"> function formatText(el,tag){ var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz if (selectedText == "") {return false} var newText='<'+tag+'>'+selectedText+'</'+tag+'>'; if(document.selection){ //IE document.selection.createRange().text=newText; } else{ //Moz el.value=el.value.substring(0,el.selectionStart)+newText+el.value.substring(el.selectionEnd,el.value.length); } } </script> <input type="button" value="B" onclick="formatText (text,'B');" style="cursor: pointer; cursor: hand"/> <input type="button" value="I" onclick="formatText (text,'I');" style="cursor: pointer; cursor: hand"/> <input type="button" value="U" onclick="formatText (text,'U');" style="cursor: pointer; cursor: hand"/> <input type="button" value="Vložiť odkaz" onclick="formatText (text,'A');" style="cursor: pointer; cursor: hand"/> <br /> <textarea id="text" name="text" style="font-family:arial; font-size = 12pt" rows = "30" cols = "120"></textarea> A chcel by som tam ešte doplniť pridanie odkazu tak ako to je na tomto fóre. Respektíve mám iba problém na vytvorenie funkcie, ktorá by mi hodila kurzor ku A =nieco.sk a žeby sa to niečo aj označilo. A potom v php funkcii by malo zrejme stačiť $text = str_replace("[A]","<a href=\"\1\">",$text); Ešte by tam bol problém s tým uzatváracím znakom. |
||
SeparateSK Profil |
Takže keď tam bude [A]niečo.sk[/A], tak to chceš nahradiť s :
<a href="nieco.sk">nieco.sk</a>.
Tak to ti postačí: $text=preg_replace("/\[A\](.*)\[/A\]/","<a href=\"$1\">$1</a>",$text); |
||
Again Profil |
#3 · Zasláno: 1. 4. 2012, 19:20:59
Smím-li dodat u preg_replace ti chybí třetí argument, takže to fungovat nebude. Já bych vyřešil celou funkci pomocí mnohem jednoduššího řešení:
<?php function makeFormattedText($text) { $code = array('~\[B\](.+)\[/B\]~' => '<b>$1</b>', '~\[A\](.+)\[/A\]~' => '<a href="$1">$1</a>', '~\[U\](.+)\[/U\]~' => '<u>$1</u>'); $text = preg_replace(array_keys($code), array_values($code), $text); return $text; } echo makeFormattedText('Tady je odkaz na [A]www.seznam.cz[/A] a také [U]podtržený text[/U].'); // volání funkce ?> Do pole $code lze samozřejmě přidat další vychytávky jako je kurzíva apod. |
||
jerremy Profil * |
#4 · Zasláno: 2. 4. 2012, 16:52:12
Again:
A ako by si urobil odkaz v tvare: odkaz ? Aby tam nebolo toto www.seznam.cz ako klikaci prvok, ale nejake iné slovo, ktoré si sam môže vybrať. Teda aby po kliknuti na odkaz sa vložilo toto čo aj tu (na tomto fore)
|
||
SeparateSK Profil |
$text=preg_replace("/\[A=(.*)\](.*)\[\/A\]/","<a href=\"$1\">$2</a>",$text); |
||
jerremy Profil * |
#6 · Zasláno: 2. 4. 2012, 22:00:17
SeparateSK:
Ak sa môžem opýtať, $1 je vlastne (.*) a $2 je to druhe (.*)? Ešte dva problémy mám s týmto scriptom. 1. Kliknúť na buttony ide, iba keď je nejaká časť textu označená. Dá sa tam dať podmienku, že ak text nie je označený tak nech to dá klasicky vedľa seba napríklad [B][/B]? 2. $text = str_replace("\n","<br>",$text); toto na Enter nefunguje, je niečo podobné aby som tam nemusel dávať tlačitko na [BR], ale aby nahradzovalo ENTER tým [BR]? |
||
SeparateSK Profil |
jerremy:
„Ak sa môžem opýtať, $1 je vlastne (.*) a $2 je to druhe (.*)?“ presne tak :) > Ešte dva problémy mám s týmto scriptom. > 1. Kliknúť na buttony ide, iba keď je nejaká časť textu označená. Dá sa tam dať podmienku, že ak text nie je označený tak nech to dá klasicky vedľa seba napríklad [>B][/>B]? Stačí odstrániť tento riadok z javascriptu: if (selectedText == "") {return false} „2. $text = str_replace("\n","<br>",$text); toto na Enter nefunguje, je niečo podobné aby som tam nemusel dávať tlačitko na [BR], ale aby nahradzovalo ENTER tým [BR]?“ použi $text=nl2br($text); |
||
jerremy Profil * |
#8 · Zasláno: 3. 4. 2012, 16:17:09 · Upravil/a: jerremy
SeparateSK:
1. if (selectedText == "") {return false} mám aj v originály, ale mi to nefunguje
2. Funkcia funguje ale len tak polovične. V databáze je to riadkované, no ak vypisujem z databázy tak to je naspäť v jednom riadku. Ospravedlnujem sa, ta druhá funkcia funguje, ďakujem :) |
||
SeparateSK Profil |
#9 · Zasláno: 3. 4. 2012, 18:01:58
jerremy:
„1. if (selectedText == "") {return false} mám aj v originály, ale mi to nefunguje“ Tento riadok máš odstrániť, ak chčeš aby to napísalo [A][/A] , aj keď text neni označený :) |
||
jerremy Profil * |
#10 · Zasláno: 3. 4. 2012, 18:42:47
Aha, prepáčte, ospravedlňujem sa za svoju hlúposť.
ďakujem |
||
Časová prodleva: 11 let
|
0