Jump to content


Photo

preg_replace question


  • Please log in to reply
7 replies to this topic

#1 b_hole

b_hole
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 14 May 2006 - 03:32 PM

I'm trying to find a way to replace any [mylist][/mylist] with <ul></ul>. That not a problem. I use preg_replace like this:
$_str=preg_replace("#[mylist](.*?)[/mylist]#", "<ul>\\1</ul>", $_str);

But I can't find a way to replace all the sub-strings </p>\r<p> inside the replaced [mylist] with </li>\r<li>. Can I do it with one preg_replace command?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 May 2006 - 04:13 PM

Change your code with this:
$_str=preg_replace("#\[mylist\](.*?)\[/mylist\]#is", "<ul>\\1</ul>", $_str);
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.

#3 b_hole

b_hole
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 14 May 2006 - 04:27 PM

[!--quoteo(post=373763:date=May 14 2006, 11:13 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ May 14 2006, 11:13 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change your code with this:
$_str=preg_replace("#\[mylist\](.*?)\[/mylist\]#is", "<ul>\\1</ul>", $_str);
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 </p>\r<p> inside every [mylist]?

#4 b_hole

b_hole
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 May 2006 - 04:53 PM

Any idea? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

#5 b_hole

b_hole
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 18 May 2006 - 05:02 PM

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?

#6 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 18 May 2006 - 06:15 PM

$_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;
[a href=\"http://php.net/preg_replace_callback\" target=\"_blank\"]preg_replace_callback()[/a]

#7 b_hole

b_hole
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 18 May 2006 - 09:03 PM

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:
some text...
[mylist]line one
line two
line three
[/mylist]
But this isn't:
some text...
[mylist]line one
line two
line three[/mylist]


Thanks alot!

#8 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 18 May 2006 - 09:48 PM

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.

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.
$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;
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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users