Author Topic: [SOLVED] highlight_string, and bbcode  (Read 1239 times)

0 Members and 1 Guest are viewing this topic.

Online The Little GuyTopic starter

  • Freak!
  • Posts: 6,103
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
[SOLVED] highlight_string, and bbcode
« on: February 06, 2007, 06:47:55 PM »
I have some bbcode, it looks like this:

Code: [Select]
$find = array('/\[page=(.*)\]/i');
$replace = array(');<h3>$1</h3>highlight_string(\'');

Code: [Select]
highlight_string(preg_replace($find,$replace,$row['code']));
It works 95% of the way, what I want is for when it finds [page=xxx] I want the highlight_string function to close, then change [page=xxx] to <h3>xxx</h3>, then open highlight_string again.

right now, it does change the [page=xxx] to the correct <h3>xxx</h3>, but it is in the highlight_string function, so instead of printing the heading to the screen, it prints <h3>xxx</h3> as code, and I don't want that.

how can I fix this?
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: highlight_string, and bbcode
« Reply #1 on: February 06, 2007, 08:42:13 PM »
Can you post the code you're parsing and the expected result? You may need to use preg_replace_callback.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Online The Little GuyTopic starter

  • Freak!
  • Posts: 6,103
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
Re: highlight_string, and bbcode
« Reply #2 on: February 06, 2007, 08:53:40 PM »
Code In the database:
Code: [Select]
[page=image.php]
<?php
header
("Content-type: image/png");
$string "abcdefghijklmnopqrstuvwxyz0123456789";
for(
$i=0;$i<6;$i++){
$pos rand(0,36);
$str .= $string{$pos};
}

$img_handle ImageCreate (6015) or die ("Cannot Create image");
//Image size (x,y)
$back_color ImageColorAllocate($img_handle121143138);
//Background color RBG
$txt_color ImageColorAllocate($img_handle000);
//Text Color RBG
ImageString($img_handle3150$number$txt_color);
Imagepng($img_handle);

session_start();
$_SESSION['img_number'] = $str;
?>


[page=form.php]
<form action="result.php">
<img alt="Random Number" src="image.php">
<input type="text" name="num"><br>
<input type="submit" name="submit" value="Check">
</form>

[page=result.php]
<?php
if($_SESSION['img_number'] != $_POST['num']){
echo'The number you entered doesn\'t match the image.';
}
?>

When displayed on the page should look like this:


image.php
<?php
header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++){
   $pos = rand(0,36);
   $str .= $string{$pos};
}

$img_handle = ImageCreate (60, 15) or die ("Cannot Create image");
//Image size (x,y)
$back_color = ImageColorAllocate($img_handle, 121, 143, 138);
//Background color RBG
$txt_color = ImageColorAllocate($img_handle, 0, 0, 0);
//Text Color RBG
ImageString($img_handle, 31, 5, 0, $number, $txt_color);
Imagepng($img_handle);

session_start();
$_SESSION['img_number'] = $str;
?>

form.php
<form action="result.php">
   <img alt="Random Number" src="image.php">
   <input type="text" name="num"><br>
   <input type="submit" name="submit" value="Check">
</form>

result.php
<?php
if($_SESSION['img_number'] != $_POST['num']){
   echo'The number you entered doesn\'t match the image.';
}
?>

each [page=xxx] Should turn into header text, not a codded version of codded text.
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: highlight_string, and bbcode
« Reply #3 on: February 06, 2007, 09:12:31 PM »
Code: [Select]
<pre>
<?php
// Where the code is for this example.
$code file_get_contents('source.txt');
// Split the data between [page] and non-[page] tags.
$code preg_split('/(^\[page=.+?\])/m'$code, -1PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_NO_EMPTY);
foreach ($code as $line) {
// For page items...
if (strpos($line'[page=') === 0) {
echo preg_replace('/^\[page=(.+?)\]/''<h3>\1</h3>'$line);
}
// For code items...
else {
highlight_string($line);
}
}
?>

</pre>
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Online The Little GuyTopic starter

  • Freak!
  • Posts: 6,103
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
Re: highlight_string, and bbcode
« Reply #4 on: February 06, 2007, 09:15:33 PM »
I'm trying to use eval(), but Im getting this error:
Parse error: syntax error, unexpected ')' in /home/.marble/ryannaddy/snippets.tzfiles.com/db.php(29) : eval()'d code on line 1

Code: [Select]
<?php
$find 
= array('/\[page=(.*)\]/i');
$replace = array(eval(");echo'<h3>$1</h3>';highlight_string(")); 

//Search database code is here (this works)

highlight_string(preg_replace($find,$replace,$row['code']));
?>
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: highlight_string, and bbcode
« Reply #5 on: February 06, 2007, 09:17:49 PM »
eval requires valid code.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Online The Little GuyTopic starter

  • Freak!
  • Posts: 6,103
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
Re: highlight_string, and bbcode
« Reply #6 on: February 06, 2007, 09:18:31 PM »
alright, thanks that works!
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting