PHP preg_match & str_replace

Hi,

I am struggling to get my brain to work today :-)

I have a textarea that uses BBCode.  When posting the textarea I use some str_replace to change the BBCode into HTML.  The problem is, I am struggling to get one of the BBCodes to work.  It is for the [code="????"][/code] BBCode.  There can be multiple instances of this in a single post.

What I need to do is take the posted textarea and for each instance of the [code] BBCode and replace each [code="????"][/code] with the relevant HTML code.

For example, say I have a string like this:

"This is my string.... This is my string.... This is my string.... This is my string....

[code="test1"]<?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?>[/code]

This is my string.... This is my string.... This is my string.... This is my string....

[code="test2"]<?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?>[/code]

This is my string.... This is my string.... This is my string.... This is my string....


What I want to do is replace this:

[code="test1"]<?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?>[/code]

with:

<div style="max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;" name="test1"><?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('test1')\">

and replace:

[code="test2"]<?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?>[/code]

with:

<div style="max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;" name="test2"><?php
// CONNECT TO DATABASE
$dbHost = "localhost";
?></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('test2')\">


and so on for as many as are in the string.

Please can someone suggest the best way to do this?

Many thanks,

John
LVL 14
john-formbyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
How about:
Find:

    /\[code="([^"]+)"\]([^\[]+)[/code]/s
    
Replace:

    "<div style=\"max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;\" name=\"$1\"><?php$2?></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('$1')\">"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
P.S.

I believe preg_replace should handle this find/replace by itself.
0
john-formbyAuthor Commented:
Hi,

I have been trying this using the following example but am struggling to get it to work.  If you copy and paste the string into the textarea and submit, it is replacing part of the string but is missing displaying the code between the [code] tags and also not replacing the [/code] section.

Many Thanks,

John
<?php
echo '<h1>Test String - Copy & Paste into Textarea</h1>';
$string = 'This is my string.... This is my string.... This is my string.... This is my string....<br /><br />

[code="test1"]&lt;?php<br />
// CONNECT TO DATABASE<br />
$dbHost = "localhost";<br />
?&gt;[/code]<br /><br />

This is my string.... This is my string.... This is my string.... This is my string....<br /><br />

[code="test2"]&lt;?php<br />
// CONNECT TO DATABASE<br />
$dbHost = "localhost";<br />
?&gt;[/code]<br /><br />

This is my string.... This is my string.... This is my string.... This is my string....';
echo $string;

echo '<h1>Result</h1>';
if(isset($_POST['submit'])) {
	$article = $_POST['article'];
	$article = preg_replace('/\[code="([^"]+)"\]([^\[]+)[\/code]/s',"<div style=\"max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;\" name=\"$1\"><pre><?php$2?></pre></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('$1')\">",$article);
	echo $article;
}
?>
<h1>Form</h1>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<textarea style="width:500px; height:200px;" name="article"></textarea><br />
<input type="submit" name="submit" value="submit" />
</form>

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

john-formbyAuthor Commented:
Ok,

I have nearly got it.  The following example is working now apart from it doesn't display the code in the div if there are square brackets in the code.  Obviously, when displaying code snippets, it is possible to have this so I need to be able to display them.  I thought about using str_replace but then it would replace the [code][/code] ones as well so it wouldn't work.

Any suggestions welcome :-)

Many thanks,

John
<html>
<head>
<title>BBCode Test</title>
<script type="text/javascript">
<!--        Script by hscripts.com          -->
<!--        copyright of HIOX INDIA         -->
<!-- Free javascripts @ http://www.hscripts.com -->
function fnSelect(objId)
{
   fnDeSelect();
   if (document.selection) 
   {
      var range = document.body.createTextRange();
      range.moveToElementText(document.getElementById(objId));
      range.select();
   }
   else if (window.getSelection) 
   {
      var range = document.createRange();
      range.selectNode(document.getElementById(objId));
      window.getSelection().addRange(range);
   }
}
function fnDeSelect() 
{
   if (document.selection)
             document.selection.empty();
   else if (window.getSelection)
              window.getSelection().removeAllRanges();
}
<!-- Script by hscripts.com -->
</script>
</head>
<body>
<?php
echo '<h1>Test String - Copy & Paste into Textarea</h1>';
$string = 'This is my string.... This is my string.... This is my string.... This is my string....<br /><br />

[code="test1"]some text<br />
&lt;?php<br />
// CONNECT TO DATABASE<br />
$dbHost = "localhost";<br />
[]
?&gt;[/code]<br /><br />

This is my string.... This is my string.... This is my string.... This is my string....<br /><br />

[code="test2"]&lt;?php<br />
// CONNECT TO DATABASE<br />
$dbHost = "localhost";<br />
?&gt;[/code]<br /><br />

This is my string.... This is my string.... This is my string.... This is my string....';
echo $string;

echo '<h1>Result</h1>';
if(isset($_POST['submit'])) {
	$article = str_replace(array("<",">"),array("&lt;","&gt;"),$_POST['article']);
	$article = nl2br(preg_replace('/\[code="([^"]+)"\]([^\[]+)\[\/code\]/s',"<div style=\"max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;\" id=\"$1\"><pre>$2</pre></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('$1')\">",$article));
	echo $article;
}
?>
<h1>Form</h1>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<textarea style="width:500px; height:200px;" name="article"></textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

Open in new window

0
VisionCommented:
Try changing your preg_replace code to this:

$article = nl2br(preg_replace('/\[code="([^"]+)"\](.*?)\[\/code\]/s',"<div style=\"max-height: 300px; overflow-y: auto; overflow-x: hidden; width: 758px; border: 1px solid #990000; background-color: #ffffff; font-family: courier; padding: 5px;\" id=\"$1\"><pre>$2</pre></div><input type=\"button\" value=\"Select All\" onClick=\"fnSelect('$1')\">",$article));

Open in new window

0
VisionCommented:
Sorry, that could have been clearer.  Change the match string to:

'/\[code="([^"]+)"\](.*?)\[\/code\]/s'

Open in new window

0
john-formbyAuthor Commented:
Perfect :-)  I figured it was that part of the pattern but wasn't sure what to change it to.

Many thanks to both of you for your help with this issue.

John
0
john-formbyAuthor Commented:
Thank you very much for your help :-)
0
käµfm³d 👽Commented:
NP. Glad to help  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.