Jump to content

[SOLVED] preg_replace and line breaks


sarathi

Recommended Posts

Here is the php I'm using to create simple bbcode tags:

 

$text="bolded text";

$tagCodes = array('/(\)(.+)(\[\/b\])/');

$replace = array('<span style="font-weight:bold;">\\2</span>');

$codedText = preg_replace($tagCodes, $replace, $text);

 

This works fine when there is one line, but when I add line breaks into it, it does not replace the tags anymore. Does anyone have any idea as to how I might fix this?  :confused:

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/
Share on other sites

i think your post may be missing a part of the regex for some reason. is this line supposed to be ...

 

$tagCodes = array('/(\[b\])(.+)(\[\/b\])/');

 

anyway, this code worked for me...

 

$text="[b]bolded[/b] text\nmore [b]bolded[/b] text";
$tagCodes = array('/(\[b\])(.+)(\[\/b\])/');
$replace = array('<span style="font-weight:bold;">\\2</span>');
$codedText = preg_replace($tagCodes, $replace, $text);
echo $codedText;

Here is the php I'm using to create simple bbcode tags:

 

$text="bolded text";

$tagCodes = array('/(\[b\])(.+)(\[\/b\])/');

$replace = array('<span style="font-weight:bold;">\\2</span>');

$codedText = preg_replace($tagCodes, $replace, $text);

 

This works fine when there is one line, but when I add line breaks into it, it does not replace the tags anymore. Does anyone have any idea as to how I might fix this?  :confused:

 

Thanks!

 

just checking, i think there was a missing backslash. i see the error when the post is in the textarea....

yes, thanks for pointing out the missing backslash.

I am try to make this work for entering the tags into a text area, and then having it posted into a div, but if I enter a line break into the text area this shows up in the div:

 

[b]bolded text
more bolded text[/b]

 

Is there a way to remove the line breaks, and then add them in after?

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.