clown[NOR] Posted May 1, 2007 Share Posted May 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
trq Posted May 1, 2007 Share Posted May 1, 2007 You can't store a function in an array, so this.... array("highlight_string(", ");"); will never work. I'm not even sure it makes sense. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 1, 2007 Author Share Posted May 1, 2007 well.. I'm still a noob on arrays and all that ... so I'm just trying and failing any ideas on how to make it work? Quote Link to comment Share on other sites More sharing options...
trq Posted May 1, 2007 Share Posted May 1, 2007 Something like... $show = preg_replace("'\[code\](.*?)\[/code\]'", highlight_string('$1'), $text); Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 1, 2007 Author Share Posted May 1, 2007 that gave me this: $1 Test: what exactly does the $1 do? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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\"; Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 <?php $s = $_REQUEST['text']; if (!empty($s)) { $show = preg_replace("'\[php\](.*?)\[/php\]'", highlight_string('$1'), $s); echo $show; } ?> Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 You can try it out here: http://oleaa.net/scripts/highlight_code.php Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 Still not sure what your talking about. I've managed to run that code through my php-cli, and its working fine for me. Good find. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 hmm... and all the colors are apearing as they should? it doesn't add lot's of \ everywhere? btw... Thanks Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Author Share Posted May 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.