bloodgoat Posted April 24, 2009 Share Posted April 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/ Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 $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); Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-818653 Share on other sites More sharing options...
bloodgoat Posted April 24, 2009 Author Share Posted April 24, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-818660 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 post the actual code you tried. I gave you an example of pattern -> replace directly with preg_replace. Looks like from your previous snippet you have an array that is being used. Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-818725 Share on other sites More sharing options...
bloodgoat Posted April 25, 2009 Author Share Posted April 25, 2009 <?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]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-819082 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-819119 Share on other sites More sharing options...
thebadbad Posted April 25, 2009 Share Posted April 25, 2009 @ 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. Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-819134 Share on other sites More sharing options...
.josh Posted April 25, 2009 Share Posted April 25, 2009 haha oops, you're right. my bad. ~@(\d+)~ Quote Link to comment https://forums.phpfreaks.com/topic/155559-solved-help-with-simple-preg_replace-quoting-system/#findComment-819141 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.