Jump to content

preg_replace help


RaythMistwalker

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.