Remove quotes from preg_replace

A

Anonymous

Guest
this is for php bb code stuff

by defualt we have this set up:
[box="Title...:"]Text orCode...[/box]
as you can see [box=""] we use quotes

when i run preg_replace the quotes in box="" remain so
we get "Title...:"

what can i add to my syntax so that preg_replace only replaces every thing except Title...:

instead of "Title...:" i want Title...:
Code:
<?php 
preg_replace(
"#\[box=(.+?)\](.+?)\[/box\]#is",
"<blockquote><b>\\1</b>\n<div class=\"box\">\\2</div>\n</blockquote>",
'[box="Title...:"]Text orCode...[/box]');
?>

i tried all kinds of things i am just not good at php syntax. thanks for your time i hope you can help me ;-)
 
Just use \" to escape the quote, like so:

Code:
<?php
$string = '[box="Title...:"]Text orCode...[/box]'; // or wherever you get that data
$string = preg_replace("#\[box=\"(.*?)\"](.*?)\[box]#is",
"<blockquote><b>\\1</b>\n<div class=\"box\">\\2</div>\n</blockquote>", $string);
// note that it should be one line but word-wrapping is weird here...
?>

Coditor
 
... man i swear i tryed that and it wouldnt work lol :( o well thanks ;-) it works now... was so easy too lol thank thanks thanks ;-)

Code:
<?php
$string = '[box="Title...:"]Text orCode...[/box]';
$string = preg_replace("#\[box=\"(.+?)\"](.+?)\[/box\]#is", "<blockquote><b>\\1</b>\n<div class=\"box\">\\2</div>\n</blockquote>", $string);
echo $string;
?>

oh ok i see what you did wrong [box] ending is open should be [/box\]
 
dam ok check this out for some reason i get wird bug when i use a form.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>untitled doc</title>
</head>
<body>
<?php 
$string = $_POST['string'];
$string1 = preg_replace('#\[box=(.+?)\](.+?)\[/box\]#is', '<blockquote><b>\\1</b><div class="box">\\2</div></blockquote>', $string);
echo $string1;
?>
<form action="index.php" method="post" enctype="application/x-www-form-urlencoded" target="_self">
<textarea name="string" cols="100" rows="10" wrap="virtual"></textarea>
<input name="" type="submit" value="Submit" />
</form>
</body>
</html>

it out puts like this:
\"Title...:\"
Text orCode...

it should be like this
Title...:
Text orCode...

if you try the above php html code, this is what the bbcode is:
[box="Title...:"]Text orCode...[/box]
 
o well i just did a cheap hack but i guess it works ;/

have to add this to end of code:
$string = str_replace('<blockquote><b>\"', '<blockquote><b>', $string);
$string = str_replace("\"</b>\n<div class=\"box\">", "</b>\n<div class=\"box\">", $string);
 
Replace line 10 of your code with this and try again:

Code:
$string = stripslashes($_POST['string']);

Coditor
 
thanks but i choose to do it like this.

Code:
<?php 
$string = str_replace('<blockquote><b>\"', '<blockquote><b>', $string);//remove left qoute
$string = str_replace("\"</b>\n<div class=\"box\">", "</b>\n<div class=\"box\">", $string);//remove right qoute
?>

Because if for some reason some one needs to type \ in there text, stripslashes will remove it. So not a good idea in this case :oops:.
 
If someone types \ in their text, it's most likely to be escaped as \\.

When a form is submitted, values are modified (slashes added) and so you should remove them when you use the data...
 
Back
Top