Bbcode needed

Please I need some bbcode to replace the following

\ \ (double backslash)
To
\ (single backslash)

Also

' to ’
Asin
I\'m the admin
Return
I' m the admin

And

" to "

\" Real Man\"
Return
"Real Man"

Isn’t that what stripslashes() does?

@Solomon Is this really a different question to the other one you’ve started? If not, then I’d suggest we get them merged into one thread to avoid confusing people.

1 Like

Also, what does this have to do with bbcode? :confused:

2 Likes

@chrisofarabia no solution was provided to the other one, so i fink its best i ask the question in a different way

@rpkamp tank for the correction, so please What is the stripslashes to do the job

For the cases you suggested strip_slashes would work yet.
Question is, how did the strings get messed up in the first place and how will you prevent it from happening again?

In that case, I’ll close the other thread. For what’s it’s worth, it would have been better to rephrase the question there if you thought it needed clarifying. One for the future though…

1 Like

Unfortunately, in starting a new thread we have to search for the PHP code you posted in that thread. I noticed that it already uses stripslashes() although I didn’t notice addslashes() anywhere - I may have missed it though.

<?php 
function atuser_search_name($user) 
{ 
$atuser_q = mysql_query("SELECT `username` FROM `users` WHERE `username`='$user'"); 
$atuser_n = mysql_num_rows($atuser_q); 
$at = mysql_fetch_object($atuser_q); 

if($atuser_n == 0) 
{ 
return "@$user"; 
} 
else 
{ 
return '@'.functions::user_link($at->username); 
} 
} 
function atuser_name($matches){ 
while($m = $matches[2]){ 
return atuser_search_name($m); 
} 
} 

function at_user($text){ 
return preg_replace_callback("/([@]+)([a-zA-z-0-9]+)/", "atuser_name", $text); 
} 

class bbcode extends core{ 
private $string; 

function __construct($text){ 
$this->string = $text; 
} 

public function hide() 
{ 
return '<blockquote>This post has been hidden</blockquote>'; 
} 


private function code($php) 
{ 
$ph=$php; 
$php = strtr($php, array('<br />' => '', '\\' => '\\')); 
$php = html_entity_decode(trim($php), ENT_QUOTES, 'UTF-8'); 
$php = substr($php, 0, 2) != "<?" ? "<?php\n" . $php . "\n?>" : $php; 
$php = highlight_string(stripslashes($php), true); 
$php = strtr($php, array('\\' => '&#92;', ':' => '&#58;', '[' => '&#91;')); 
return "<div class='code'><code>$php</code><br /><b><u><font color='red'>Copy Code:</font><br /></u></b><textarea cols='30'>$ph</textarea></div>"; 
} 



private function linkify($value, $protocols = array('http', 'mail', 'https'), array $attributes = array(), $mode = 'normal') 
{ 
// Link attributes 
$attr = ''; 
foreach ($attributes as $key => $val) { 
$attr = ' ' . $key . '="' . htmlentities($val) . '"'; 
} 

$links = array(); 

// Extract existing links and tags 
$value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value); 
// Extract text links for each protocol 
foreach ((array)$protocols as $protocol) { 
switch ($protocol) { 
case 'http': 
case 'https': 
$value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . $link . '</a>') . '>'; }, $value); break; 
case 'mail': 
$value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; 
case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); 
break; 
default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i' : '~([^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; 
} 
} 

// Insert all link 
return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); 
} 

private function bbcode($string){ 
$string = preg_replace("(\[b\](.+?)\[\/b])is",'<b>$1</b>',$string); 
$string = preg_replace("(\[br\])is",'<br>',$string); 
$string = preg_replace("(\[hr\])is",'<hr>',$string); 
$string = preg_replace("(\[i\](.+?)\[\/i])is",'<i>$1</i>',$string); 
$string = preg_replace("(\[s\](.+?)\[\/s])is",'<s>$1</s>',$string); 
$string = preg_replace("(\[left\](.+?)\[\/left])is",'<left>$1</left>',$string); 
$string = preg_replace("(\[right\](.+?)\[\/right])is",'<right>$1</right>',$string); 
$string = preg_replace("(\[center\](.+?)\[\/center])is",'<center>$1</center>',$string);
$string = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<font size='$1'>$2</font>",$string); 
$string = str_replace('\r\n', '<br>', $string); 
$string = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<font color='$1'>$2</font>",$string); 
$string = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])is","<font face='$1'>$2</font>",$string); 
$string = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $string); 
$string = preg_replace("/\[img=(.+?)\]\[\/img\]/", '<img src="$1">', $string); 
$urlstring = " a-zA-Z0-9\:\&\/\-\?\.\_\=\~\#\'\%"; 
$string = preg_replace("(\[url\]([$urlstring]*)\[/url\])", '<a href="$1" target="_blank">$1</a>', $string); 
$string = preg_replace("(\[email\](.+?)\[\/email])is",'<email>$1</email>',$string);
$string = preg_replace("/\[youtube](.+?)\[\/youtube\]/", ' 
<div class="iframe"> 
<iframe width="96%" src="http://www.youtube.com/embed/$1?modestbranding=1" frameborder="0" allowfullscreen="yes"></iframe> 
</div> 
', $string); 

$string = preg_replace("(\[sub\](.+?)\[\/sub])is",'<sub>$1</sub>',$string); 
$string = preg_replace("(\[sup\](.+?)\[\/sup])is",'<sup>$1</sup>',$string); 
$string=preg_replace(array('#\[code\](.+?)\[\/code\]#se'), array("''.$this->code('$1').''"), str_replace("]\n", "]", $string)); 
$string = preg_replace("(\[quote\](.+?)\[\/quote])is","<blockquote>$1</blockquote>",$string); 
$string = preg_replace("(\[quote author=(.+?)\](.+?)\[\/quote\])is","<blockquote><b>$1:</b>$2</blockquote>",$string); 
$string=nl2br($string); 
return $string; 
} 



private function smiley($string) 
{ 
$smiles=array(':)'=>"<img src='/smileys/smiley.gif' alt=':)'/>", 
';)'=>"<img src='/smileys/wink.gif' alt=';)'/>", 
':D'=>"<img src='/smileys/cheesy.gif' alt=':D' />", 
';D'=>"<img src='/smileys/grin.gif' alt=';D' />", 
'>:('=>"<img src='/smileys/angry.gif' alt='>:(' />", 
':('=>"<img src='/smileys/sad.gif' alt=':(' />", 
':o'=>"<img src='/smileys/shocked.gif' alt=':o' />", 
'8)'=>"<img src='/smileys/cool.gif' alt='8)' />", 
'???'=>"<img src='/smileys/huh.gif' alt='???' />", 
':P'=>"<img src='/smileys/tongue.gif' alt=':P' />", 
':-['=>"<img src='/smileys/embarassed.gif' alt=':-[' />", 
':-X'=>"<img src='/smileys/lipsrsealed.gif' alt=':-X' />", 
':-\\'=>"<img src='/smileys/undecided.gif' alt=':-\\' />", 
':-*'=>"<img src='/smileys/kiss.gif' alt=':-*' />", 
':::('=>"<img src='/smileys/cry.gif' alt=':::(' />", 
'(stop)'=>"<img src='$config->url/addons/smileys/stop.gif' alt='(stop)' />", 
'(1st)'=>"<img src='$config->url/addons/smileys/1st.gif' alt='(1st)' />",
'(2nd)'=>"<img src='$config->url/addons/smileys/2nd.gif' alt='(2nd)' />", 
'(3rd)'=>"<img src='$config->url/addons/smileys/3rd.gif' alt='(3rd)' />", 
'(best)'=>"<img src='$config->url/addons/smileys/best.gif' alt='(best)' />", 
'(block)'=>"<img src='$config->url/addons/smileys/block.gif' alt='(block)' />", 
'(clap)'=>"<img src='$config->url/addons/smileys/clap.gif' alt='(clap)' />", 
'(cool)'=>"<img src='$config->url/addons/smileys/cool.gif' alt='(cool)' />", 
'(thumbsup)'=>"<img src='$config->url/addons/smileys/thumbsup.gif' alt='(thumbsup)' />", 
'(blackhat)'=>"<img src='$config->url/addons/smileys/blackhat.gif' alt='(blackhat)' />", 
'(devilish)'=>"<img src='$config->url/addons/smileys/devilish.gif' alt='(devilish)' />", 
'(superman)'=>"<img src='$config->url/addons/smileys/superman.gif' alt='(superman)' />", 
'(tongueup)'=>"<img src='$config->url/addons/smileys/tongueup.gif' alt='(tongueup)' />", 
'(welcome2)'=>"<img src='$config->url/addons/smileys/welcome2.gif' alt='(welcome2)' />", 
'(yeye)'=>"<img src='$config->url/addons/smileys/yeye.gif' alt='(yeye)' />", 
'(barca)'=>"<img src='$config->url/addons/smileys/barca.jpeg' alt='(barca)' />", 
'(cheesy)'=>"<img src='$config->url/addons/smileys/cheesy.gif' alt='(cheesy)' />", 
'(cool)'=>"<img src='$config->url/addons/smileys/cool.gif' alt='(cool)' />", 
'(cry)'=>"<img src='$config->url/addons/smileys/cry.gif' alt='(cry)' />", 
'(drunk)'=>"<img src='$config->url/addons/smileys/drunk.gif' alt='(drunk)' />", 
'(goodnight)'=>"<img src='$config->url/addons/smileys/goodnight.gif' alt='(goodnight)' />", 
'(huh)'=>"<img src='$config->url/addons/smileys/huh.gif' alt='(huh)' />", 
'(lies)'=>"<img src='$config->url/addons/smileys/lies.gif' alt='(lies)' />", 
'(liverpool)'=>"<img src='$config->url/addons/smileys/liverpool.gif' alt='(liverpool)' />", 
'(lol)'=>"<img src='$config->url/addons/smileys/lol.png' alt='(lol)' />", 
'(love)'=>"<img src='$config->url/addons/smileys/love.png' alt='(love)' />", 
'(love2)'=>"<img src='$config->url/icons/new.gif' alt='(love2)' />", 
'(madrid)'=>"<img src='$config->url/addons/smileys/real.png' alt='(madrid)' />", 
'(br)'=>"<br/><br/>", 
'(br/)'=>"<br/>", 
'(tongue)'=>"<img src='$config->url/addons/smileys/tongue.gif' alt='(tongue)' />", 
'(nicethread)'=>"<img src='$config->url/addons/smileys/nicethread.gif' alt='(nicethread)' />"); 

$string=str_replace(array_keys($smiles), array_values($smiles), $string); 
return $string; 
} 
public function display() { 
return $this->linkify(at_user($this->bbcode($this->smiley($this->string)))); 
} 

} 
?>

That is my bbcode.php

When i type on single backslash, the result will return two backslash.

For instance

Am using \ once

Will return

Am using \ once (showing 2 )

How can i remove this backslash from my forum.?

I’ve moved the last two posts from that topic over here.

2 Likes

@Gandalf @chrisofarabia
So no solution to the additional backslash in my post.
All my post look nasty with backslash everywhere

As suggested by @droopsnoot, using stripslashes() should remove the added slashes.

I don’t think the code you posted contains the code you need to change. You have functions in there to display code (presumably formatted code as you can do in here), to translate links, to translate smileys and bbcode tags, but the code to just retrieve normal text from the database doesn’t seem to be there.

The key might be:

class bbcode extends core{ 

and maybe the code for dealing with standard strings that don’t need special processing is in core.

1 Like

One thing is certain, the escape slashes are getting added somewhere.

I agree that seeing them in the rendered content is undesirable.

But I am not sure that the assumption the bbcode script is at fault is correct.

I’m guessing the process is as follows.

  • a user submits text which is then “cleaned up” - script tags removed etc.
  • the “clean” user supplied input is then entered into the database.
  • the data is returned from the database, passed to the bbcode script to change bbcode tags to HTML and used to output the page.

Escape slashes might be getting added in any of those steps. I suspect the problem is because they are getting added in two places but are getting removed in only one.

Because the code is using mysql which has reached End Of Life, IMHO it would be better to find some code that uses mysqli or PDO rather than spend much time troubleshooting the out-of-date code.

In any case, you should “backtrack” to see where the escape slashes first make their appearance.

That is, if you look directly at the database field content are there no, one or two slashes?

eg.
O'Reilly - O\'Reilly - O\\'Reilly - O&#92;'Reilly

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.