Jump to content

Archived

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

b_hole

preg_replace question

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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]?

Share this post


Link to post
Share on other sites
Any idea? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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 one
line two
line three
[/mylist][/code]
But this isn't:
[code]some text...
[mylist]line one
line two
line three[/mylist][/code]


Thanks alot!

Share this post


Link to post
Share on other sites
[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 one
line two
line three[/mylist]
dskjdksds
[mylist]
dksldksd
sdkslkd
[/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.

Share this post


Link to post
Share on other sites

×

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.