b_hole Posted May 14, 2006 Share Posted May 14, 2006 I'm trying to find a way to replace any [b][mylist][/mylist][/b] with [b]<ul></ul>[/b]. That not a problem. I use preg_replace like this:[code]$_str=preg_replace("#[mylist](.*?)[/mylist]#", "<ul>\\1</ul>", $_str);[/code]But I can't find a way to replace all the sub-strings [b]</p>\r<p>[/b] inside the replaced [mylist] with [b]</li>\r<li>[/b]. Can I do it with one preg_replace command? Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/ Share on other sites More sharing options...
wildteen88 Posted May 14, 2006 Share Posted May 14, 2006 Change your code with this:[code]$_str=preg_replace("#\[mylist\](.*?)\[/mylist\]#is", "<ul>\\1</ul>", $_str);[/code]Now the PCRE engine will treat any instace of [ or ] as-is as. [ and ] have a special meaning and so you need to tell the PCRE engine to treat those characters as-is. Also note that that added teo characters after the last # in your regualar expressins. This tells the PCRE engine to ingore newline characters and to be case-insensitive too. Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-35728 Share on other sites More sharing options...
b_hole Posted May 14, 2006 Author Share Posted May 14, 2006 [!--quoteo(post=373763:date=May 14 2006, 11:13 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ May 14 2006, 11:13 AM) [snapback]373763[/snapback][/div][div class=\'quotemain\'][!--quotec--]Change your code with this:[code]$_str=preg_replace("#\[mylist\](.*?)\[/mylist\]#is", "<ul>\\1</ul>", $_str);[/code]Now the PCRE engine will treat any instace of [ or ] as-is as. [ and ] have a special meaning and so you need to tell the PCRE engine to treat those characters as-is. Also note that that added teo characters after the last # in your regualar expressins. This tells the PCRE engine to ingore newline characters and to be case-insensitive too.[/quote]My mistake, of course I'm using \ before [. Anyway, case-sensitivity is not important at the moment, and I definitely don't want to ignore new-lines.Is it possible to change [b]</p>\r<p>[/b] inside every [mylist]? Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-35732 Share on other sites More sharing options...
b_hole Posted May 15, 2006 Author Share Posted May 15, 2006 Any idea? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-36020 Share on other sites More sharing options...
b_hole Posted May 18, 2006 Author Share Posted May 18, 2006 I guess I'm lost here. Everything I tried didn't worked, and I can't find anything in www.php.net.I have wildteen88's code, but I can't think of a way to replace a specific text inside the ul (</p>\r<p>), and still leave everything eles as-is.Can someone point me to the solution? Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-36921 Share on other sites More sharing options...
shoz Posted May 18, 2006 Share Posted May 18, 2006 [code]$_str = <<<LLL[mylist]<p>point 1</p><p>point 2</p>[/mylist]skldslkdsd[mylist]<p>list1</p><p>list2</p>[/mylist]LLL;function make_list($matches){ $_str = '<ul>'.preg_replace('#<p>(.*?)</p>#is', "<li>\\1</li>", $matches[1]).'</ul>'; return $_str;}$_str=preg_replace_callback("#\[mylist\](.*?)\[/mylist\]#is", 'make_list', $_str);print $_str;[/code][a href=\"http://php.net/preg_replace_callback\" target=\"_blank\"]preg_replace_callback()[/a] Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-36963 Share on other sites More sharing options...
b_hole Posted May 18, 2006 Author Share Posted May 18, 2006 That's nice, thanks shoz [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Do you have an idea how to deal situations when there is no </p><p> at the last li?What I'm doing is adding <p> at the beginning of the text and </p> at the end of the text, replacing every new line with </p><p> and then replacing [mylist] with <ul>, just like you suggested. The problem is that I can't tell for sure that there is new line at the end of the last li.This is OK:[code]some text...[mylist]line oneline twoline three[/mylist][/code]But this isn't:[code]some text...[mylist]line oneline twoline three[/mylist][/code]Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-37001 Share on other sites More sharing options...
shoz Posted May 18, 2006 Share Posted May 18, 2006 [quote]What I'm doing is adding <p> at the beginning of the text and </p> at the end of the text, replacing every new line with </p><p> and then replacing [mylist] with <ul>, just like you suggested.[/quote]I assume you're only referring to my suggestion about the [mylist] to <ul><li> replacement.If you're saying you'd like text on every line between [mylist][/mylist] to be between <p></p> then you can do the following.[code]$str = <<<LLL[mylist]line oneline twoline three[/mylist]dskjdksds[mylist]dksldksdsdkslkd[/mylist]LLL;function make_list($matches){ return '[mylist]'.preg_replace('#^(.+)$#im', '<p>\\1</p>', $matches[1]).'[/mylist]';}$str=preg_replace_callback("#\[mylist\](.*?)\[/mylist\]#is", 'make_list', $str);print $str;[/code]However, if you're ultimately going to be converting everything to <ul><li></ul>, you can do it all at once by changing [mylist] to <ul> and <p></p> to <li></li> in the code found in the "make_list" function. Quote Link to comment https://forums.phpfreaks.com/topic/9657-preg_replace-question/#findComment-37010 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.