Page 1 of 1

preg_replace() isn't working like I think it should ^^;

Posted: Sat Mar 12, 2005 5:35 pm
by Mini-Me
:x this is weird. I'm writing (well, actually have written) a wiki. I'm basically using bbcode to format the stuff. Trouble is, I can't see why the list part isn't working...
Here's the text file:

Code: Select all

<ul>
<li>test</li>
</ul>
<br /><i><b>test</b></i>
<br />
<br /><div class=&quote;code&quote;><b>test</b>
<br /><i>test</i>
<br /><b><i>test</i></b></div>
This SHOULD translate to this:

Code: Select all

їlist]
ї*]testї/*]
ї/list]
їi]їb]testї/b]ї/i]

їcode]їb]testї/b]
їi]testї/i]
їb]їi]testї/i]ї/b]ї/code]
In actuality, it translates to this:

Code: Select all

<ul>
<li>test</li>
</ul>
їi]їb]testї/b]ї/i]

їcode]їb]testї/b]
їi]testї/i]
їb]їi]testї/i]ї/b]ї/code]
Here's the relavent code (I'm including the smilie part in case that's part of the problem):

Code: Select all

<?php
function parsefrom($txt) {
  $preg_good = array(
      "[url=\\1]\\2[/url]",
      "[img]\\1[/img]",
      "[b]\\1[/b]",
      "[u]\\1[/u]",
      "[i]\\1[/i]",
      "[s]\\1[/s]",
      "

Code: Select all

\\1
",
"[color=\\1]\\2[/color]",
"[size=\\1]\\2[/size]",
"parselist_back('\\1');" // << what it should do afterwards
);
$smile_g = array(
":)",
":(",
";)",
":D",
":'(",
":P",
":-/",
"O_O"
);
$preg_bad = array(
"/<a href=\"(.+?)\" target=\"_blank\">(.+?)<\/a>/i",
"/<img src=\"(.+?)\">/i",
"/<b>(.+?)<\/b>/i",
"/<u>(.+?)<\/u>/i",
"/<i>(.+?)<\/i>/i",
"/<span style=\"text-decoration: line-through;\">(.+?)<\/span>/i",
"/<div class=\"code\">(.+?)<\/div>/i",
"/<span style=\"color: (.+?);\">(.+?)<\/span>/i",
"/<span style=\"font-size: (.+?)px;\">(.+?)<\/span>/i",
"/<ul>(.+?)<\/ul>/ie" // << line in question
);
$smile_b = array(
"/<img src=\"wiki\/smile.gif\" \/>/i",
"/<img src=\"wiki\/sad.gif\" \/>/i",
"/<img src=\"wiki\/wink.gif\" \/>/i",
"/<img src=\"wiki\/grin.gif\" \/>/i",
"/<img src=\"wiki\/cry.gif\" \/>/i",
"/<img src=\"wiki\/tongue.gif\" \/>/i",
"/<img src=\"wiki\/puzzled.gif\" \/>/i",
"/<img src=\"wiki\/confused.gif\" \/>/i"
);
$txt = preg_replace($preg_bad, $preg_good, $txt);
$txt = preg_replace($smile_b, $smile_g, $txt);
$bad = array('&', '<', '>', '"');
$good = array('&', '<', '>', "\"");
$txt = str_replace($bad, $good, $txt);
return str_replace('<br />', "\n", $txt);
}
function parselist_back($meh) {
$meh = "
  • " . $meh . "
";
return preg_replace("/<li>(.*?)<\/li>/i", "[*]\\1[/*]", $meh);
}

It's got to be some small error I'm making, but I just can't see it.
Anyone know anything?

Posted: Sat Mar 12, 2005 6:15 pm
by Chris Corbyn
Nope it works.
Tested and got:

Code: Select all

&#1111;list]

      &#1111;*]test&#1111;/*] 

&#1111;i]&#1111;b]test&#1111;/b]&#1111;/i]
&#1111;b]test&#1111;/b] &#1111;i]test&#1111;/i] &#1111;b]&#1111;i]test&#1111;/i]&#1111;/b]
&#1111;/list]
Not sure where the indent has come from in the output though :?

EDIT: My bad... it doesn't work but that's a different output to yours.

I'll look at this for you :wink:

Posted: Sat Mar 12, 2005 6:54 pm
by Mini-Me
THAT's weird. I've tried this over and over again with different stuff and always get the html. Just double-checked it again too. I can't see anything else in my code that would affect it...
These are the only other lines that have to do with it:

Code: Select all

$content = file_get_contents('wiki/' . $_REQUEST['pg'] . '.txt');
//...
$content = parsefrom($content);
//...
print('...'.$content.'...');

Posted: Sat Mar 12, 2005 7:11 pm
by Chris Corbyn
Exactly what I did. I do get some html sorry. But still it's not what you posted. I'll work this out. Sorry haven't looked at it much. Been busy on some client work over deadline :(

Code: Select all

&#1111;list]<ul>
&#1111;*]test&#1111;/*]
</ul>

&#1111;i]&#1111;b]test&#1111;/b]&#1111;/i]



<div class=&quote;code&quote;>&#1111;b]test&#1111;/b]

&#1111;i]test&#1111;/i]

&#1111;b]&#1111;i]test&#1111;/i]&#1111;/b]</div>&#1111;/list]

Posted: Sat Mar 12, 2005 7:15 pm
by Skara
(Skara = Mini-Me... the Mini-Me account goofed)

I figured it out. ^_^ ..well, I was actually just browsing the old threads here for no other reason than I'm bored and came across what the s meant.

Code: Select all

//...
"/<ul>(.+?)<\/ul>/ies"
//...
The s at the end for not stopping at line breaks. I have NO idea why it needed it here and not in any other tags, but it works now. ^^;

Posted: Sat Mar 12, 2005 7:27 pm
by Chris Corbyn
Nice one. Well done! :lol: