IronWarrior Posted August 31, 2008 Share Posted August 31, 2008 Here I have a function: function paragraphConvert($vehicleDescription) { $string = $vehicleDescription; $toReplace[0] = '/[p]/'; $toReplace[1] = '/[/p]/'; $toReplace[2] = '/[u]/'; $toReplace[3] = '/[/u]/'; $replacement[0] = '<p>'; $replacement[1] = '</p>'; $replacement[2] = '<u>'; $replacement[3] = '</u>'; echo preg_replace($toReplace, $replacement, $string); } And here I have an error: Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\AppServ\www\tanksforsale\siteConfiguration.php on line 112 Basically If I have a block of text like this: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque viverra. Vestibulum lorem. Cras laoreet suscipit lacus. Maecenas bibendum quam nec enim. Quisque luctus eleifend lorem. Sed massa ante, varius ac, elementum at, congue nec, lorem. Duis mi. Vivamus dolor justo, convallis at, dignissim quis, posuere interdum, mi. Sed euismod lacus vel tortor. In lacus pede, adipiscing in, auctor sed, molestie sit amet, turpis. Nunc hendrerit gravida nunc. Sed libero pede, convallis sit amet, tristique non, vulputate nec, lorem. Etiam ut arcu at pede consectetuer condimentum. Pellentesque ante odio, aliquam varius, fermentum vitae, sagittis ac, leo. Vestibulum placerat scelerisque felis. Integer felis justo, interdum a, accumsan non, venenatis ut, felis. Mauris ut ante. Proin eu purus. Nunc ornare laoreet neque. Duis id erat. Curabitur est neque, tristique ac, molestie nec, blandit vel, pede. Nulla facilisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas porta eros a mi. Vestibulum cursus luctus eros. Fusce metus ipsum, pellentesque ac, sollicitudin at, auctor eget, neque. Sed quis odio. Maecenas at lacus sed libero ultricies aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus sapien metus, rutrum in, placerat sed, laoreet ultrices, arcu. Phasellus sodales metus et enim. Morbi hendrerit, leo et accumsan placerat, nunc nibh sollicitudin metus, sed consectetuer purus sem at ligula. Quisque nec ligula at velit sodales blandit. Cras id purus. Mauris turpis tortor, interdum a, vestibulum eget, consequat vitae, magna. Nunc pellentesque purus vel felis. Cras dapibus. In ullamcorper molestie felis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sed justo. Vestibulum ac eros sed massa imperdiet pellentesque. Suspendisse potenti. I add it to a database and it turns out like this: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque viverra. Vestibulum lorem. Cras laoreet suscipit lacus. Maecenas bibendum quam nec enim. Quisque luctus eleifend lorem. Sed massa ante, varius ac, elementum at, congue nec, lorem. Duis mi. Vivamus dolor justo, convallis at, dignissim quis, posuere interdum, mi. Sed euismod lacus vel tortor. In lacus pede, adipiscing in, auctor sed, molestie sit amet, turpis. Nunc hendrerit gravida nunc. Sed libero pede, convallis sit amet, tristique non, vulputate nec, lorem. Etiam ut arcu at pede consectetuer condimentum. Pellentesque ante odio, aliquam varius, fermentum vitae, sagittis ac, leo. Vestibulum placerat scelerisque felis. Integer felis justo, interdum a, accumsan non, venenatis ut, felis. Mauris ut ante. Proin eu purus. Nunc ornare laoreet neque. Duis id erat. Curabitur est neque, tristique ac, molestie nec, blandit vel, pede. Nulla facilisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas porta eros a mi. Vestibulum cursus luctus eros. Fusce metus ipsum, pellentesque ac, sollicitudin at, auctor eget, neque. Sed quis odio. Maecenas at lacus sed libero ultricies aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus sapien metus, rutrum in, placerat sed, laoreet ultrices, arcu. Phasellus sodales metus et enim. Morbi hendrerit, leo et accumsan placerat, nunc nibh sollicitudin metus, sed consectetuer purus sem at ligula. Quisque nec ligula at velit sodales blandit. Cras id purus. Mauris turpis tortor, interdum a, vestibulum eget, consequat vitae, magna. Nunc pellentesque purus vel felis. Cras dapibus. In ullamcorper molestie felis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sed justo. Vestibulum ac eros sed massa imperdiet pellentesque. Suspendisse potenti. Is there anyway I can combat this other than using my above (as yet not working) function? Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/ Share on other sites More sharing options...
wildteen88 Posted August 31, 2008 Share Posted August 31, 2008 And here I have an error: Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\AppServ\www\tanksforsale\siteConfiguration.php on line 112 That's because you should escape the square brackets ( '[' and ']' ). Also it is pointless using preg_replace if you're not using regex within your patterns. This is how I'd do bbcodes: function bbcode($string) { $search[0] = '#\[p\](.*?)\[/p\]#ies'; $search[1] = '#\[u\](.*?)\[/u\]#is'; $replace[0] = "'<p>'.nl2br('$1').'</p>'"; $replace[1] = '<u>$1</u>'; return preg_replace($search, $replace, $string); } $text = '[p]Paragraph 1[/p] [p]Paragraph 2, with [u]Underline![/u][/p] [p]Paragraph 3, keeps newlines![/p]'; echo bbcode($text); Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/#findComment-630362 Share on other sites More sharing options...
.josh Posted August 31, 2008 Share Posted August 31, 2008 I don't think he's trying to do bb codes I think he's trying to keep his text from losing his html tags so he thinks if he replaces them with non-html tags they will be marked and he can later convert them back...which is in essence what bb code does, but I think he's just wanting to preserve his format. Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/#findComment-630365 Share on other sites More sharing options...
wildteen88 Posted August 31, 2008 Share Posted August 31, 2008 I don't think he's trying to do bb codes I think he's trying to keep his text from losing his html tags so he thinks if he replaces them with non-html tags they will be marked and he can later convert them back...which is in essence what bb code does, but I think he's just wanting to preserve his format. Ohh, after re-reading the post I think you are right. @IronWarrior ignore my post. The problem you're having is to do with the web browser ignoring newlines in your formatted text. To prevent this you need to convert the newlines in your text to a line break tag (<br />). This can be achieved by using a built in function called nl2br. You should use this function when you retrieve data from your database only. Example $sql = 'SELECT * FROM your_table'; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { echo nl2br($row['your_field_with_formatted_text']); } nl2br should not be used when adding data to the databases, as you may find your newlines duplicate each time you edit the text. Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/#findComment-630375 Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 Wildteen, how come you guys hid the [nobbc] tag from us? D: That would have made my life so much easier when posting <br />. Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/#findComment-630381 Share on other sites More sharing options...
IronWarrior Posted August 31, 2008 Author Share Posted August 31, 2008 That's a great method, did start by using your BBcode function, this also works damn well. Thanks for all your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/#findComment-630384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.