Jump to content

Recommended Posts

I have the following code for BBCodes:

 

$bbreplace = array (
          '/(\[[bb]\])(.+)(\[\/[bb]\])/',
          '/(\[[ii]\])(.+)(\[\/[ii]\])/',
          '/(\[[uu]\])(.+)(\[\/[uu]\])/',
          '/(\[[ss]\])(.+)(\[\/[ss]\])/',
          '/(\[url=http://)(.+)(\])(.+)(\[\/url\])/',
          '/(\[img\])(.+)(\[\/img\])/',
          '/(\[img\])(.+)(\[\/IMG\])/',
          '/(\[color=)(.+)(\])(.+)(\[\/COLOR\])/',
          '/(\[color=)(.+)(\])(.+)(\[\/color\])/',
          '/(\[size=)(.+)(\])(.+)(\[\/SIZE\])/',
          '/(\[size=)(.+)(\])(.+)(\[\/size\])/',
          '/(\[list\])(.+)(\[\/list\])/',
          '/(\[list=1\])(.+)(\[\/list\])/',
          '/(\[list\])(.+)(\[\/LIST\])/',
          '/(\[list=1\])(.+)(\[\/LIST\])/',
          '/(\[*\])(.+)()/'
          );
      $bbreplacements = array (
          '<b>\\2</b>',
          '<i>\\2</i>',
          '<u>\\2</u>',
          '<s>\\2</s>',
          '<a href="\\2">\\4</a>',
          '<img src="\\2">',
          '<img src="\\2">',
          '<font color="\\2">\\4</font>',
          '<font color="\\2">\\4</font>',
          '<font size="\\2">\\4</font>',
          '<font size="\\2">\\4</font>',
          '<ul>\\2</ul>',
          '<ol>\\2</ol>',
          '<ul>\\2</ul>',
          '<ol>\\2</ol>',
          '<li>\\2</li>'
          );
      $PostText = preg_replace($bbreplace, $bbreplacements, $PostText);

 

Now first,

 

Say I enter:

[b]Something
then this on a different line[/b]

 

It returns:

[b

  • Something

then this on a different line[/b

 

 

 

Now if I use

[list]
[*]List 
[*]List 
[/list]

[list=1]
[*]Order 
[*]List 
[/list]

 

It puts:

 

 

[list

 

 

[*

  • List

 

[*

  • List

 

[/list

 

 

 

  • Order

 

[*

  • List

 

[/list ]

 

 

Any Ideas on this?

Link to comment
https://forums.phpfreaks.com/topic/250322-preg_replace-help/
Share on other sites

I didn't get the same result on the first example, but there may have been a problem with greediness and the fact that dot . does not match a newline unless you use the s modifier (I also used i for case insensitivity):

 

$bbreplace = array (
          '#\[b\](.*)\[/b\]#is',
          );
$bbreplacements = array (
          '<b>\\1</b>',
          );

Link to comment
https://forums.phpfreaks.com/topic/250322-preg_replace-help/#findComment-1284415
Share on other sites

Ok Using your method the Mutli-line bold etc now works.

 

New Code:

      $bbreplace = array (
          '#\[b\](.*)\[\/b\]#is',
          '#\[i\](.*)\[\/i\]#is',
          '#\[u\](.*)\[\/u\]#is',
          '#\[s\](.*)\[\/s\]#is',
          '#\[url=http://(.*)\](.+)\[\/url\]#is',
          '#\[img\](.*)\[\/img\]#is',
          '#\[color=(.*)\](.*)\[\/color\]#is',
          '#\[size=(.*)\](.*)\[\/size\]#is',
          '#\[list\](.*)\[\/list\]#is',
          '#\[list=1\](.*)\[\/list\]#is',
          '#\[*\](.+)#is'
          );

 

And the replace with:

      $bbreplacements = array (
          '<b>\\1</b>',
          '<i>\\1</i>',
          '<u>\\1</u>',
          '<s>\\1</s>',
          '<a href="\\1">\\2</a>',
          '<img src="\\1">',
          '<font color="\\1">\\2</font>',
          '<font size="\\1">\\2</font>',
          '<ul>\\1</ul>',
          '<ol>\\1</ol>',
          '<li>\\1</li>'
          );

 

Bit the list function still doesn't work. Using same as above now returns:

[*
List 
[*]List 
[/list]

[list=1]
[*]Order 
[*]List 

 

All as 1 list item.

Link to comment
https://forums.phpfreaks.com/topic/250322-preg_replace-help/#findComment-1284421
Share on other sites

Ok Progress being made. Repacing my one with your one now returns:

 

  • List
  • List
    [/ list]
     
    [ list=1]
  • Order
  • List

 

Edit: spaces added to make correct ones show up. This still doesnt end first list and then start an ordered list.

Link to comment
https://forums.phpfreaks.com/topic/250322-preg_replace-help/#findComment-1284799
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.