sarathi Posted July 29, 2009 Share Posted July 29, 2009 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? Thanks! Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/ Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 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; Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/#findComment-885648 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 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? Thanks! just checking, i think there was a missing backslash. i see the error when the post is in the textarea.... Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/#findComment-885649 Share on other sites More sharing options...
sarathi Posted July 29, 2009 Author Share Posted July 29, 2009 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? Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/#findComment-885658 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 actually you can just add the modifier "s" at the end of the regex, which will also match newlines... $tagCodes = array('/(\[b\])(.+)(\[\/b\])/s'); just learned this myself, good to know... Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/#findComment-885666 Share on other sites More sharing options...
sarathi Posted July 29, 2009 Author Share Posted July 29, 2009 That works perfectly, thank you for the help fooDigi! Link to comment https://forums.phpfreaks.com/topic/167911-solved-preg_replace-and-line-breaks/#findComment-885667 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.