[Risolto] - php e js - codice php dentro js

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Scrivi risposta
treled
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1331
Iscrizione: lunedì 26 aprile 2010, 17:36
Desktop: gnome
Distribuzione: ubuntu 23.04/22.04
Sesso: Maschile
Località: Massa(MS)

[Risolto] - php e js - codice php dentro js

Messaggio da treled »

Salve, perché se metto il codice php dentro ad questo javascript .. non mi funziona il codice php ?

Come posso risolvere il problema?

Codice: Seleziona tutto

<?php
session_start();
include("inc.php"); 
include("admin_mod.php"); 
?>	<!DOCTYPE html>
	<html lang="it">
	<meta charset="utf-8">
	<link href="../css/f.css" rel="stylesheet" type="text/css">
	<link href="css/preview.css" rel="stylesheet" type="text/css">
	<link href="../css/extra.css" rel="stylesheet" type="text/css">
    <body onload="initializeMainDiv();">
	<?php 
		$bb = new BBCode();	
		$content = '<div id="f"><div id="mainDiv"></div></div>';
		echo $bb->bbcode_complete($content);
	?>
    <script>
	function initializeMainDiv() {
        document.getElementById("mainDiv").innerHTML = "" +
		window.opener.document.post.content.value;
    };
	</script>
</body>
</html>
idea?

aspetto risposta e ringrazio e buona serata.
Ultima modifica di treled il venerdì 13 marzo 2015, 9:32, modificato 1 volta in totale.
Avatar utente
SuperStep
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 2037
Iscrizione: lunedì 19 dicembre 2011, 16:26
Desktop: Unity
Distribuzione: Ubuntu 16.04 LTS x86_64
Sesso: Maschile
Località: Somma Vesuviana (NA)

Re: php e js - codice php dentro js

Messaggio da SuperStep »

qual'e' l'errore?
ubuntu 16.04 LTS 64-bit - Memoria: 31,3 Gib - Processore: Intel Core i7-5960X CPU @ 3.00 GHz × 16 - Grafica: AMD Radeon HD 7800 Series - Disco: SSD 256 GB x 4 (RAID 01)
Avatar utente
Eresia
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 362
Iscrizione: venerdì 30 giugno 2006, 1:20
Distribuzione: gentoo
Sesso: Maschile

Re: php e js - codice php dentro js

Messaggio da Eresia »

li non vedo nessun codice php dentro ad un javascript. Anche perchè non potresti, puoi passare variabili tipo:

Codice: Seleziona tutto

<script>
   var php_var = "<?php echo $php_var; ?>";
</script>
o

Codice: Seleziona tutto

var php_var=<?php echo json_encode($php_var); ?>;
Dovresti dare più informazioni sul codice
emerge --auD --oneshot life/lucky-*
treled
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1331
Iscrizione: lunedì 26 aprile 2010, 17:36
Desktop: gnome
Distribuzione: ubuntu 23.04/22.04
Sesso: Maschile
Località: Massa(MS)

Re: php e js - codice php dentro js

Messaggio da treled »

ok, non da nessun errore.. ma non mi renderizza più il bbcode.. o meglio visualizza il testo come fosse normale esempio:

Codice: Seleziona tutto

[ b ]grassetto[ /b] [ color=red ] rosso  [ / color]
idea?

la pagina viene richiamata da una pagina principale e quella è un popup window child.
il codice in questione normale lo trovato qui:

https://www.ibm.com/developerworks/comm ... t5?lang=en


grazie mille.
Avatar utente
Eresia
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 362
Iscrizione: venerdì 30 giugno 2006, 1:20
Distribuzione: gentoo
Sesso: Maschile

Re: php e js - codice php dentro js

Messaggio da Eresia »

Sarebbe da vedere la funzione php!
Ma per renderizza intendi bold o italic senza i tag?
http://kuikie.com/snippet/90-32/php/str ... e-to-html/
emerge --auD --oneshot life/lucky-*
treled
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1331
Iscrizione: lunedì 26 aprile 2010, 17:36
Desktop: gnome
Distribuzione: ubuntu 23.04/22.04
Sesso: Maschile
Località: Massa(MS)

Re: php e js - codice php dentro js

Messaggio da treled »

ciao io uso questo :
https://github.com/kaimallea/php-bbcode

perché ha anche le cose per youtube..

e in più ho aggiunto dei tag in più che ho fatto io....

vedi immagine allegata, mi appare cosi..
http://uppix.com/f-155001cf900187f39.png

PS: uppix servizio hosting immagini..

grazie mille.
Avatar utente
Eresia
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 362
Iscrizione: venerdì 30 giugno 2006, 1:20
Distribuzione: gentoo
Sesso: Maschile

Re: php e js - codice php dentro js

Messaggio da Eresia »

Codice: Seleziona tutto

<?php
require "bbcode.php";


$bbcode = new BBCode;
echo $bbcode->toHTML("[b]prova[/b]");  
?>
cosi funziona (come da script)

cos'hai modificato?
Hai incluso il file bbcode.php (non lo vedo nel tuo esempio)

che funzione è questa?

Codice: Seleziona tutto

bbcode_complete($content);
emerge --auD --oneshot life/lucky-*
treled
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1331
Iscrizione: lunedì 26 aprile 2010, 17:36
Desktop: gnome
Distribuzione: ubuntu 23.04/22.04
Sesso: Maschile
Località: Massa(MS)

Re: php e js - codice php dentro js

Messaggio da treled »

ok, ecco il codice.. mi hanno aiutato ad fare il famoso "Leggi tutto".. in stile wordpress (più/meno).

Codice: Seleziona tutto

<?php
class BBCode {
	
  protected $bbcode_table = array();
  
  public function __construct () {
    
    // Replace [b]...[/b] with <strong>...</strong>
    $this->bbcode_table["/\[b\](.*?)\[\/b\]/is"] = function ($match) {
      return "<strong>$match[1]</strong>";
    };


    // Replace [i]...[/i] with <em>...</em>
    $this->bbcode_table["/\[i\](.*?)\[\/i\]/is"] = function ($match) {
      return "<em>$match[1]</em>";
    };


    // Replace [code]...
with <pre><code>...</code></pre>
$this->bbcode_table["/\[code\](.*?)\[\/code\]/is"] = function ($match) {
return "<pre><code>$match[1]</code></pre>";
};


// Replace [quote]...[/quote] with <blockquote><p>...</p></blockquote>
$this->bbcode_table["/\[quote\](.*?)\[\/quote\]/is"] = function ($match) {
return "<blockquote><p>$match[1]</p></blockquote>";
};


// Replace [quote="person"]...[/quote] with <blockquote><p>...</p></blockquote>
$this->bbcode_table["/\[quote=\"([^\"]+)\"\](.*?)\[\/quote\]/is"] = function ($match) {
return "$match[1] wrote: <blockquote><p>$match[2]</p></blockquote>";
};


// Replace ... with <span style="font-size:30%">...</span>
$this->bbcode_table["/\[size=(\d+)\](.*?)\[\/size\]/is"] = function ($match) {
return "<span style=\"font-size:$match[1]%\">$match[2]</span>";
};


// Replace with <del>
$this->bbcode_table["/\[s\](.*?)\[\/s\]/is"] = function ($match) {
return "<del>$match[1]</del>";
};


// Replace ... with <span style="text-decoration:underline;">...</span>
$this->bbcode_table["/\[u\](.*?)\[\/u\]/is"] = function ($match) {
return '<span style="text-decoration:underline;">' . $match[1] . '</span>';
};


// Replace [center]...[/center] with <div style="text-align:center;">...</div>
$this->bbcode_table["/\[center\](.*?)\[\/center\]/is"] = function ($match) {
return '<div style="text-align:center;">' . $match[1] . '</div>';
};

// Replace [right]...[/right] with <div style="text-align:right;">...</div>
$this->bbcode_table["/\[right\](.*?)\[\/right\]/is"] = function ($match) {
return '<div style="text-align:right;">' . $match[1] . '</div>';
};

// Replace [left]...[/left] with <div style="text-align:left;">...</div>
$this->bbcode_table["/\[left\](.*?)\[\/left\]/is"] = function ($match) {
return '<div style="text-align:left;">' . $match[1] . '</div>';
};


// Replace [justify]...[/justify] with <div style="text-align:justify;">...</div>
$this->bbcode_table["/\[justify\](.*?)\[\/justify\]/is"] = function ($match) {
return '<div style="text-align:justify;">' . $match[1] . '</div>';
};


// Replace ... with <span style="color:somecolor">...</span>
$this->bbcode_table["/\[color=([#a-z0-9]+)\](.*?)\[\/color\]/is"] = function ($match) {
return '<span style="color:'. $match[1] . ';">' . $match[2] . '</span>';
};


// Replace [email]...[/email] with <a href="mailto:...">...</a>
$this->bbcode_table["/\[email\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[1]</a>";
};


// Replace An e-mail link with <a href="mailto:someone@somewhere.com">An e-mail link</a>
$this->bbcode_table["/\[email=(.*?)\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[2]</a>";
};


// Replace ... with <a href="...">...</a>
$this->bbcode_table["/\[url\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[1]</a>";
};

// Replace [url_blank]...[/url_blank] with <a href="...">...</a>
$this->bbcode_table["/\[url_blank\](.*?)\[\/url_blank\]/is"] = function ($match) {
return "<a href=\"$match[1]\" target=\"_blank\">$match[1]</a>";
};

// Replace A link to google with <a href="http://www.google.com/">A link to google</a>
$this->bbcode_table["/\[url=(.*?)\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[2]</a>";
};


// Replace [img]...[/img] with <img src="..."/>
$this->bbcode_table["/\[img\](.*?)\[\/img\]/is"] = function ($match) {
return "<img src=\"$match[1]\"/>";
};


// Replace
  • ...
with <ul><li>...</li></ul>
$this->bbcode_table["/\[list\](.*?)\[\/list\]/is"] = function ($match) {
$match[1] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
}, $match[1]);

return "<ul>" . preg_replace("/[\n\r?]/", "", $match[1]) . "</ul>";
};


// Replace
  • ...
with <ul|ol><li>...</li></ul|ol>
$this->bbcode_table["/\
  • (.*?)\[\/list\]/is"] = function ($match) {
    if ($match[1] == '1') {
    $list_type = '<ol>';
    } else if ($match[1] == 'a') {
    $list_type = '<ol style="list-style-type: lower-alpha">';
    } else {
    $list_type = '<ol>';
    }

    $match[2] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
    return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
    }, $match[2]);

    return $list_type . preg_replace("/[\n\r?]/", "", $match[2]) . "</ol>";
    };


    // Replace with <iframe src="..."></iframe>
    $this->bbcode_table["/\[youtube\](?:http?:\/\/)?(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=)([A-Z0-9\-_]+)(?:&(.*?))?\[\/youtube\]/i"] = function ($match) {
    return "<iframe class=\"youtube-player\" type=\"text/html\" width=\"340\" height=\"285\" src=\"http://www.youtube.com/embed/$match[1]\" frameborder=\"0\"></iframe>";
    };
    }

    public function replace ($str, $escapeHTML=false, $nr2br=false) {
    if (!$str) {
    return "";
    }

    if ($escapeHTML) {
    $str = htmlspecialchars($str);
    }

    foreach($this->bbcode_table as $key => $val) {
    $str = preg_replace_callback($key, $val, $str);
    }

    return nl2br($str);
    }

    function bbcode_more($stringa, $id)
    {
    if(strpos($stringa,"[/MORE]")){
    $str=explode("[/MORE]",$stringa);
    if(trim($str[0]) !="[MORE]"){//la stringa[0] è formata da [MORE] + qualcos'altro
    $dopo = trim(str_replace($str[0], "<div class='post_links_more'><p><a href='news_view.php?id=$id'>Leggi Tutto</a></p></div>", $str[0]));
    $prima=str_replace("[MORE]","",$str[0]);
    //Ritorno il valore
    return $prima."... ".$dopo;
    }else{//la stringa[0] è formata solo da [MORE]
    $stringa=str_replace("[MORE]","",$stringa);
    $stringa=str_replace("[/MORE]","",$stringa);
    return $stringa;
    }
    }else{
    return $stringa;//se non c'è [MORE] deve tornare la stringa
    }
    }
    function bbcode_complete($string)
    {
    $string = $this->replace($string);//richiami la funzione bbcode
    $string = trim(str_replace("[MORE]", "", $string));
    $string = trim(str_replace("[/MORE]", "", $string));
    //Ritorno il valore
    return $string;
    }
    }
    ?>
    [/code]

    EDIT : il file è incluso nel file inc.php.

    idea e grazie mille.
Avatar utente
Eresia
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 362
Iscrizione: venerdì 30 giugno 2006, 1:20
Distribuzione: gentoo
Sesso: Maschile

Re: php e js - codice php dentro js

Messaggio da Eresia »

Codice: Seleziona tutto

   /* funzione read more */
	function read_more($string, $num_char, $link_more){
		$string = strip_tags(html_entity_decode($string));
		if( strlen($string) <= $num_char ){
			$result = $string;
		} else {
			$result = "";
			$words = explode(" ", $string);
			foreach( $words as $x ){
				if( strlen($result . " " . $x) < $num_char ){
					if( isset($result) ) $result .= " ";
					$result .= $x;
				} else {
					break;
				}
			}
			$result .= $link_more;
		}
		return $result;
	}
//

Codice: Seleziona tutto

<?php
require "bbcode2.php";

$text = 'Lorem ipsum dolor sit amet, 
consectetur adipiscing elit, sed do eiusmod tempor incididunt 
ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
commodo consequat. Duis aute irure dolor in reprehenderit in 
voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
Excepteur sint occaecat cupidatat non proident, 
sunt in culpa qui officia deserunt mollit anim id est laborum.';

$bbcode = new BBCode;
echo $bbcode->read_more($text, 100, ' <a href="link/al/testo/completo.html">[leggi tutto]</a>');  
?>

$text = testo completo
100 = numero caratteri // intero modificabile
e il 3° parametro al link, che puoi tranquillamente cambiare con $id
emerge --auD --oneshot life/lucky-*
treled
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1331
Iscrizione: lunedì 26 aprile 2010, 17:36
Desktop: gnome
Distribuzione: ubuntu 23.04/22.04
Sesso: Maschile
Località: Massa(MS)

Re: php e js - codice php dentro js

Messaggio da treled »

che c'entra quello?

comunque ho risolto.. ho fatto diversamente..

ho solo che quando inserisco la salvo come bozza e poi dopo quando ho tutto completato, la pubblico.

Grazie mille e buona giornata.
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 19 ospiti