Jump to content

Auto insert highlight_string();


clown[NOR]

Recommended Posts

Basicly what I'm trying to do, is like on this forum.. everything that is between [ code ] & [/ code ] will endup between the highlight_string(); function... but i cant get it to work.. seems like the code gets removed...

 

this is what I got so far

<?php

if (isset($_REQUEST['Submit'])) {

	$text = $_REQUEST['text'];

	echo $text."<br><br>";

	$repArray = array("[code]", "

");

$repArray1 = array("highlight_string(", ");");

 

$show = str_replace($repArray, $repArray1, $text);

echo $show;

 

}

 

?>

<form method="post" name="test" action="highlight_code.php">

 

<textarea name="text" cols="50" rows="10"></textarea>

<br />

<input type="submit" name="Submit" value="Send" />

 

</form>

[/code]

 

this is what it outputs:

Test [ code ] [/ code ] hehe

 

Test highlight_string( ); hehe

 

Thanks In Advance!

- Clown

Link to comment
Share on other sites

ok... i've made it... well, almost made it...

 

<?php
$s = $_REQUEST['text'];
if (!empty($s)) {		
	$s = str_replace("]\n", "]", $s);
	$match = array('#\[php\](.*?)\[\/php\]#se');
	$replace = array("highlight_string(stripslashes('$1'), true)");
	$show = preg_replace($match, $replace, $s);
	echo $show;
}
?>

 

but it returns this:

<?php 

    function checkIP($ip) 
    { 
        $lines = file('archive/projects/ipcount/ips.txt'); 
        foreach ($lines as $line) { 
            if (trim($line) == $ip) 
                return true; 
        } 
    } 
     
    function countIP() 
    { 
        $ip = $_SERVER['REMOTE_ADDR']; 
        $chkIP = checkIP($ip); 
        if (!$chkIP) { 
            $write = $ip."\n\"; 
            $fh = fopen('archive/projects/ipcount/ips.txt', 'a+'); 
            fwrite($fh, $write); 
            fclose($fh); 
            return calcIP(); 
        } else { 
            return calcIP(); 
        } 
         
    } 
     
    function calcIP() 
    { 
        $lines = file('archive/projects/ipcount/ips.txt'); 
        $i = 1; 
        foreach ($lines as $line) { 
            $i++; 
        } 
        return $i-1; 
    } 
     
?>

 

here's the problem... how can i remove the last '\' from the output?

$write = $ip."\n\"; 

Link to comment
Share on other sites

that didn't work... sorry.. all i got was this:

 

 

but i did figure out the issue on the other one I posted... it's the "" around highlight... inside the array()

 

any ideas on how to go around that?

 

cuz if I change:

$write = $ip."\n";

 

to

$write = $ip.'\n';

 

everything works fine

but I cant only use the ' hehe

Link to comment
Share on other sites

but i did figure out the issue on the other one I posted... it's the "" around highlight... inside the array()

 

No kidding. And, as I said in my first reply, you cannot store a function call within an array. highlight_string() was missing an argument.

 

<?php
  $s = $_REQUEST['text'];
  if (!empty($s)) {		
    $show = preg_replace("'\[php\](.*?)\[/php\]'", highlight_string('$1',true), $s);
    echo $show;
  }
?>

 

Should work. Ive no access to my server ATM to test, but it looks ok.

Link to comment
Share on other sites

yes you can.. it did work didn't it? and that code was found on php.net with some modifications to fit my use...

 

and on php.net it was inside the array()

 

btw... it was not missing an argument... if that was the case I would have gotten an error message wouldn't I?

Link to comment
Share on other sites

What are you rambling about? highlight_string() has an optional return argument that (if set) returns the string instead of printing it. It was missing.

 

Is your problem solved? Mark the thread so.

Link to comment
Share on other sites

no my problem aint solved.. and please bare with me.. I'm still an extreme rookie on this... it's just that... the code that I found on php.net almost works.. but yours dont... here's the code directly from php.net

 

<?php
function bbcode($s)
{
    $s = str_replace("]\n", "]", $s);
    $match = array('#\[php\](.*?)\[\/php\]#se');
    $replace = array("'<div>'.highlight_string(stripslashes('$1'), true).'</div>'");
    return preg_replace($match, $replace, $s);
}
?>

 

but the problem is the slashes... after I've done for example an echo it keeps on showing the rest of the code in red.. how can I stop that?

Link to comment
Share on other sites

well... I've removed the array() from both places making the code look like this...

 

<?php
$s = $_REQUEST['text'];
if (!empty($s)) {		
	$s = str_replace("]\n", "]", $s);
	$match = '#\[code\](.*?)\[\/code\]#se';
	$highlight = "'<div>'.highlight_string(stripslashes('$1'), true).'</div>'";
	echo preg_replace($match, $highlight, $s);
}
?>

 

but the outcome is still not right:

<?php 
    $s = $_REQUEST['text']; 
    if (!empty($s)) {         
        $s = str_replace("]\n\", \"]\", $s); 
        $match = array('#\[code\](.*?)\[\/code\]#se'); 
        $replace = array(\"'<div>'.highlight_string(stripslashes('$1'), true).'</div>'\"); 
        echo preg_replace($match, $replace, $s); 
    } 
?>

 

EDIT: WTF... it happened again...why is my codes been printed out like that?

Link to comment
Share on other sites

the script only works as it should for me if i strip off all slashes.. that's the only way I can get the colors to appear as they should.. but if I dont do that, everything from the str_replace is red... it's like it's missing a quote, but it's not.. It's the extra slashes that's added that messes it up

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.