• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[PHP] help using brackets as text

ok, here is an example:

$search = "/[BOLD]/"
$replace = "<b>"

preg_replace($search, $replace, $str);
I get

Warning: Unknown modifier 'B'

how can I get it so it reads a bracket as text and not as a modifier thingy :confused:
$search = "/[BOLD]/"; 
$replace = "<b>";
preg_replace($search, $replace, $str);
I think your problem lies in the fact that you don't have ; after each finished line of code. You can also try adding a \ before each bracket, so you would get "/\[BOLD\]/".

(Thanks btw, you solved a problem I had for a long time with PHP :D Though very simple, I just couldn't get myself to use preg_replace :doh:
oops yea, I had ; there, just not when I retyped it :p , but I have another question.

if I have "/[/CODE]/"

how can I make that work, like [ CODE ] begins code and [ /CODE ] ends code?

Keep getting a Warning: not a modifier '/'
Same thing, add a \ before the /, like "/\[\/CODE\]/". Damn, that are a lot of slashes :p A \ means PHP should output/use the character, even when it's a special character, like [, /, etc.
the \ is the escape character which means that php or the preg should treat that character as a literal with no special attributes. The \ is special so if you want a literal one you will need to use \\

I think preg_quote() will do this to a certain extent, you might want to write a little test file to see how it changes strings.
ok, I got most of it work excecpt there is a little bug in my code
function bracket2html($str)
   $search[0]  = "/\[CODE\](.+)\[\/CODE\]/";           $replace[0]  = "<br /><div style=\"width: 95%; padding: 0px 5px 10px 5px; background-color: #999999; border: 1px solid #404040;\"><font style=\"font-family: Courier new;\">\\1</font></div><br />";
   //some more $search and $replace arrays

   $str = preg_replace($search, $replace, $str);
   $str = str_replace("\n", "<br />", $str);
   return $str;
it will find the [ CODE ] correctly but when trying to get the [ /CODE ], it wil go all the way to the last [ /CODE ] in the $str. How can I make it stop when it finds the first [ /CODE ]? :confused:

*edit* here is the rest of the search/replace array:
   $search[1]  = "/\[SNIP\](.+)\[\/SNIP\]/";
      $replace[1]  = "<font style=\"font-family: courier new;\">\\1</font>";
   $search[2]  = "/\[B\](.+)\[\/B\]/";
      $replace[2]  = "<b>\\1</b>";
   $search[3]  = "/\[BOLD\](.+)\[\/BOLD\]/";
      $replace[3]  = "<b>\\1</b>";
   $search[4]  = "/\[U\](.+)\[\/U\]/";
      $replace[4]  = "<u>\\1</u>";
   $search[5]  = "/\[UNDERLINE\](.+)\[\/UNDERLINE\]/";
      $replace[5]  = "<u>\\1</u>";
   $search[6]  = "/\[LINK=\"(.+)\"\](.+)\[\/LINK\]/";
      $replace[6]  = "<a href=\"\\1\">\\2</a>";
   $search[7]  = "/\[IMG=\"(.+)\"\]/";
      $replace[7]  = "<img src=\"images/\\1\" />";
they all dont work :mad:
Now I need your help:
function bbCodeconverter($text){
  $search[0] = "/\[[bB]\](.*?)\[\/[bB]*\]/";
  $replace[0] = "<b>\\1</b>";
  $search[1] = "/\[img\](.*?)\[\/img]/";
  $replace[1] = "<img src=\"\\1\">";

  $text = preg_replace($search,$replace,$text);
  $text = str_replace("\n","<br>",$text);
  return $text;
As you can see, I modified the thingy to look for both the lowercase and uppercase version of b, using [bB]. I want to have the same using img and IMG. I tried using "/\[[img|IMG]\](.*?)\[\/[img|IMG]\]/", but that just doesn't work, all it does is splurting out the raw text I just sent to the replacerfunction.

Can anyone help me? While your at it, how can I adjust the parser so it would look for strings like iMG, imG, ImG etc? Or can you only to that by entering all possible combinations manually in the $search[x] thingy?
Yes, but that would make the complete text lowercase, wouldn't it? But I can convert it back using one of the other functions, but what if someone uses capatalization in the middle of the sentence (like 'I', an abbreviation, shouting etc), the system doesn't know what words are in caps.
Is it possible to make only the converted text in lowercase, so it will keep the rest of the caps intact?


Chamone M*tha Fu*ka
Use the "i" keyword to turn case sensitivity off:
$string = preg_replace('!('.$keyword.')!i','<span style="color:red">$1</span>',$string); 
echo $string;

echo sql_regcase("img"); 
// [Ii][Mm][Gg] 
damn you, you beat me to the 'i' thing :p
yeah just put the a lowercase i after your closing character for the preg

ie "/\[B\]/i"

anyways, please use <strong> instead of <b> for the HTML tag

*I got beaten so had to offer up something ;)
Glaanieboy said:
Thank you both :)
@bunny: Why should I use <strong>? <b> does the same for me, and it is less typing :p
<b> is depreciated and may be removed from the HTML spec sometime in the future

<strong> for bold
<em> for italics
And again, a question.
I have this portion of search and replace:
$search[] = "/\[url\](.*?)\[\/url\]/i";
$replace[] = "<a href=\"\\1\">\\1</a>";
I have created this function to strip loooooong uri's (same thing this board does when you are posting something like [ url]http://loooooooooooooooooooooooooooooooooooooongurl.com[ /url], it's stripping the long text, but it's keeping the URI itself intact for proper linking). This is how the function looks like:
function strLongURI($uri){
  $uri_len = strlen($uri);
  if ($uri_len >= 15){
    $first_part = substr($uri,0,7);
    $second_part = substr($uri,-7);
    $str_uri = $first_part."..".$second_part;
    $str_uri = $uri;
  return $str_uri;
But how can I implement this in the $replace[] thingy? I have tried using $replace[] = "<a href=\"\\1\">".strLongURI('\\1')."</a>"; But that does nothing, it's just showing the full URI. How can I use functions in combination with these regular expressions?

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
What a long strange trip it's been. =)

Forum statistics

Latest member