Page 1 of 1

preg_replace

Posted: Sat Aug 07, 2004 6:06 pm
by ol4pr0
Oke.. i have the following wich kinda works however not what i want or need.

Code: Select all

$message = nl2br($file_content);
$pattern = array("#<br />#","#<u>#","#</u>#","#<b>#","#</b>#");
$replacement = "";
echo '<TEXTAREA NAME="" ROWS="20" COLS="50">';
echo preg_replace($pattern, $replacement, $message);
echo '</TEXTAREA>';
This will replace all with 'nothing' basicly, but what if i want to replace only <br /> with nothing and the others with for example.

Posted: Sat Aug 07, 2004 6:09 pm
by feyd
make an array of $replacement.

Posted: Sat Aug 07, 2004 11:47 pm
by ol4pr0
Thanks..

Oke i got it working for replacing with </b>

however how to replace
[.color= red] sometext[./color]

with
<font color="red">some text</font>

Another thing:

The following is not working nicely lol it wont insert with the html tags
has a nice effect tho jejejeje

Code: Select all

<?
 $message = nl2br($row_index&#1111;'bodyContent']);
 $pattern = array("#<br />#","#<u>#","#</u>#","#<b>#","#</b>#");
 $replacement = array("","&#1111;u]","&#1111;/u]","&#1111;b]","&#1111;/b]");
 echo preg_replace($pattern, $replacement, $message);
 echo '</textarea>';
 echo '<INPUT TYPE="submit" name="submit" value="Display">';
 echo '</form>';
 echo '</span></td>';
 mysql_free_result($index);
 if (isset($_POST&#1111;'submit']))
 &#123;
 $pattern = array("#&#1111;u]#","#&#1111;/u]#","#&#1111;b]#","#&#1111;/b]#");
 $replacement = array("<u>","</u>","<b>","</b>");
 $replace_text = nl2br(preg_replace($pattern, $replacement, $message));
 require_once($_SERVER&#1111;'DOCUMENT_ROOT'] .'/Connections/fd.php'); 
 mysql_select_db($database_fd, $fd);
 $query = "UPDATE indexcontent SET bodyContent='".$update_text."' WHERE pageID='".$_GET&#1111;'action']."'";
 $result=mysql_query($query);
 echo $query;
 &#125;

Posted: Sun Aug 08, 2004 12:37 am
by feyd
[] are special tags in preg_* functions... you need to escape them. As for doing the color code.. download phpbb and look at their bbcode script.

Posted: Sun Aug 08, 2004 12:39 am
by ol4pr0
Any short explanation on how i should escape them?


And maby some on the color thing?



Edit:

Code: Select all

got it :)
$pattern = array("#\&#1111;u]#","#\&#1111;/u]#","#\&#1111;b]#","#\&#1111;/b]#");

Help on the color thing would be nice tho

Posted: Sun Aug 08, 2004 12:42 am
by feyd
just download the bbcode script that phpbb uses...

Posted: Sun Aug 08, 2004 12:46 am
by ol4pr0
Thats a hell of a scirpt to downlaod, and to search true lol..

You might find that very easy, but i do not.

Posted: Sun Aug 08, 2004 12:48 am
by feyd
bbcode.php wrote:

Code: Select all

$text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);

yeah realllll hard... :?

Posted: Sun Aug 08, 2004 1:11 am
by ol4pr0
This is kinda my first real day that i am messing with it. So what is easy for you might not be as easy for other people.

Could you be so nice which page ?

Thanks very much anyway.

Posted: Sun Aug 08, 2004 1:14 am
by feyd
bbcode.php.. in phpbb's code tree..

Posted: Sun Aug 08, 2004 3:20 am
by timvw
Or get the PEAR::HTML_BBCode package. Can be easily adapted to run without all those other pear thiingies..