[PHP] help using brackets as text

CHiLLaXen

hypnotika
Joined
6 Jan 2004
Messages
107
ok, here is an example:

Code:
$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:
 
Code:
$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
PHP:
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:
PHP:
   $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:
 
YES! I found out why it wasn't working. instead of using (.+) I have to use (.*?)
 
Now I need your help:
Code:
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?
 
Convert the case of the string as the first line:
$text = strtolower($text)
then look for everything in lowercase
 
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?
 
Use the "i" keyword to turn case sensitivity off:
Code:
<?php 
$keyword='iMg'; 
$string='img'; 
$string = preg_replace('!('.$keyword.')!i','<span style="color:red">$1</span>',$string); 
echo $string;
?>

Or

Code:
<?php 
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 ;)
 
Thank you both :)
@bunny: Why should I use <strong>? <b> does the same for me, and it is less typing :p
 
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
 
I don't know if you have the answer, but why? b=bold and i=italic, simple, right? Why do thay make it so difficult :(
 
The adding of the 'i' was what I was looking for (I just tested it). Thanks :)
 
And again, a question.
I have this portion of search and replace:
Code:
$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:
Code:
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;
  }else{
    $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 profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back