Jump to content

[SOLVED] Help with simple preg_replace quoting system


bloodgoat

Recommended Posts

Quoting feature of my shoutbox... I still don't understand preg_replace for the life of me, and I can't get this one to work.

 

All shouts are marked with <a name="id"> where id is the post number of the particular shout. To quote or reference someone, I want the person to be able to type "@22" for example, and it would link to shoutbox.php#22 and show the 22nd post.

 

This is what I have:

<?php
$bbcodes[] = array("#\@(.*?)#i", "<a href=\"#$1\">@$1</a>");
?>

Only the "@" is being anchored in the href tags, and it's just linking to "#" which is, as we all know, the top of the page. How do I fix this?

Link to comment
Share on other sites

$string = preg_replace('~@\d+~',"<a href='#$1'>@$1</a>",$string);

 

Although, if I remember correct, starting an id with a number is not allowed or else not conforming to the standards. You might want to prefix your anchor id's. Maybe something like this:

 

<a name='post_xx'>...

 

and then

 

$string = preg_replace('~@\d+~',"<a href='#post_$1'>@$1</a>",$string);

 

Link to comment
Share on other sites

$string = preg_replace('~@\d+~',"<a href='#$1'>@$1</a>",$string);

 

Although, if I remember correct, starting an id with a number is not allowed or else not conforming to the standards. You might want to prefix your anchor id's. Maybe something like this:

 

<a name='post_xx'>...

 

and then

 

$string = preg_replace('~@\d+~',"<a href='#post_$1'>@$1</a>",$string);

I meant the id of the name="" value was the post number, not that it was using the actual id="" tag. But thanks, I'll try this

 

Edit: Same thing is happening. It's only anchoring the # symbol in the actual href="" portion, but instead of displaying a linkable @ symbol as well as the originally posted number after it in-between the <a> and </a> tags, the numbers have vanished. Still kind of at square one.

Link to comment
Share on other sites

<?php

// BBCode function
function BBCode($post){
$post = htmlentities($post);   

$bbcodes = array();
$bbcodes[] = array("~@\d+~", "<a href=\"#post_$1\">@$1</a>");
$bbcodes[] = array("#\[b\](.*?)\[/b\]#is", "<b>$1</b>");
$bbcodes[] = array("#\[i\](.*?)\[/i\]#is", "<i>$1</i>");
$bbcodes[] = array("#\[url=(.*?)\](.*?)\[/url\]#i", "<a href=\"$1\" target=\"_blank\">$2</a>");
$bbcodes[] = array("#\[url\](.*?)\[/url\]#i", "<a href=\"$1\" target=\"_blank\">$1</a>");
$bbcodes[] = array("#\:as\:#i", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/as.png\">");
$bbcodes[] = array("#\:a-boo\:#i", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/boo_as.png\">");
$bbcodes[] = array("#\:a-link\:#i", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/link_as.png\">");
$bbcodes[] = array("#\:a-snake\:#i", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/snake_as.png\">");

foreach($bbcodes as $replace){
	$post = preg_replace($replace[0], $replace[1], $post);
}
return $post;
}

// And when the posts are called
for($i=0;$i<count($post);$i++){
$post[$i][3] = BBCode($post[$i][3]);
}

?>

Link to comment
Share on other sites

To be honest you'd be better with something like:

 

function BBCode($post){
   $post = htmlentities($post);   

   $bbcodes = array();
   $bbcodes[] = "/@(\d+?)/";
   $bbnext[] =  "<a href=\"#post_$1\">@$1</a>";
   $bbcodes[] = "#\[b\](.*?)\[/b\]#is";
   $bbnext[] =  "<b>$1</b>";
   $bbcodes[] = "#\[i\](.*?)\[/i\]#is";
   $bbnext[] =  "<i>$1</i>";
   $bbcodes[] = "#\[url=(.*?)\](.*?)\[/url\]#i";
   $bbnext[] =  "<a href=\"$1\" target=\"_blank\">$2</a>";
   $bbcodes[] = "#\[url\](.*?)\[/url\]#i";
   $bbnext[] =  "<a href=\"$1\" target=\"_blank\">$1</a>";
   $bbcodes[] = "#\:as\:#i";
   $bbnext[] =  "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/as.png\">";
   $bbcodes[] = "#\:a-boo\:#i";
   $bbnext[] = "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/boo_as.png\">";
   $bbcodes[] = "#\:a-link\:#i";
   $bbnext[] =  "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/link_as.png\">";
   $bbcodes[] = "#\:a-snake\:#i";
   $bbnext[] = "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/snake_as.png\">";
   preg_replace($bbcodes,$bbnext,$post);
   return $post;
}

Link to comment
Share on other sites

@ Crayon

You forgot to capture the digit. And in regard to the naming, it's okay to start names with digits.

 

Edit: But in XHTML the name attribute is 'formally deprecated', and id is used instead. And when styling elements via the id attribute, it has to start with a letter. So you were on to something :)

 

@OP

When you run htmlentities() on $post, aren't @s converted? If yes, that's where your problem lies.

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.