preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

preg_replace

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make an array of $replacement.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just download the bbcode script that phpbb uses...
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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... :?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bbcode.php.. in phpbb's code tree..
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Or get the PEAR::HTML_BBCode package. Can be easily adapted to run without all those other pear thiingies..
Post Reply