note 69398 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 20:02:57 +0000 |
| Subject: | note 69398 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127702@lists.php.net to get a copy of this message | ||
Note Submitter: Vadtec
----
Sevendust at aaoclan dot be, you have an error in your reqex array. At least in the $BBCode array.
Look at $BBCode[1].
$BBCode[1] = '/[/b]/';
To the regex engine, this will appear as /[/b with everthing after the b becoming useless
information. This is because you enclosed the regex in / /, as you should. There is a simple
solution to this, either do not put the regex in / / which turns the string into a literal regex
(will match only what is in quotes), or escape the extra /, as in '/[\/b]/'. However, to
also be sure that the regex matches the [ ] and properly, you should also escape them, as in
'/\[\/b\]/', which is increditbly ugly.
In your example, it would be much simpler to simply quote the regex without / /. This forces the
regex engine to look *only* for that string and replace it. I tried using your class last night and
kept getting errors such as "Unknown modifier 'b' in <file here> on line
X". After removing the / / from the qouted string, it worked great.
As for my reason using preg_replace(), I had a much more challenging task. I wanted to be able to
match BB code with its start and end BB tags to convert it to HTML for display, but also to convert
the HTML back into BB code if necessary. I did this so that I could store the HTML code directly in
a DB rather than having to parse the code as I queried the text from the DB. I know this is rather
compicated, but I will need to be able to pull the text from the DB without the BB code for display
on a remote site which would not have my BB code to decode the BB tags. (Plus, I just wanted to see
if I could do it anyways.)
Assume that you are using CSS to format the elements of a page. Using <span
class="whatever">whatever</span> is an excellent way to do this. So assume you
have a CSS class called "term" and you want to use the BB tag [t]whatever[/t] to mark it,
but also want the code to replace <span class="term">whatever</span> with the
proper BB tags. Using the method from Sevendust, you can only go from BB tags to HTML tags, not HTML
tags to BB tags. This is because tags like </span> could apply to any number of BB tags.
So, after messing with the regexes for a while, I came up with this solution.
<?php
function convert_bb($text) {
$temp = split("\n", $text);
$bb_code[0] = '/(\[t\])(.*)(\[\/t\])/'; // Notice how you have to escape each regex
identifier so that it will match literally.
$bb_replace[0] = '<span class="term">${2}</span>';
$temp2 = preg_replace($bb_code, $bb_replace, $temp);
$temp2 = implode("\n", $temp2);
return $temp2;
}
function convert_html($text) {
$temp = split("\n", $text);
$html_code[0] = '/(<span class="term">)(.*)(<\/span>)/';
$html_replace[0] = '[t]${2}[/t]';
$temp2 = preg_replace($html_code, $html_replace, $temp);
$temp2 = implode("\n", $temp2);
return $temp2;
}
?>
Yes, I know its ugly and hard to read the regexes, but it will match the start and end of and set of
BB or HTML tags and covert them accordingly. As you can see, the BB tags will be matched like so:
[t](any ammount of text here)[/t]
will be replaced with
<span class="term">(whatever text was between the [t] and [/t] tags)</span>
Going from HTML tags to BB tags works exactly in reverse:
<span class="term">(any ammount of text here)</span>
will be will be replaced with
[t](whatever text was between the <span calss="term"> and </span> tags)[/t]
Note that if you want the regex to match just <span> you will have to change it to just
/(<span>)(.*)(<\/span>)/.
Put this in a PHP script and give it a try. For every regex you add to the $bb_code/$bb_replace set,
it will convert that BB tag set to its corresponding HTML tag set while preserving whatever is
between the BB tags, and vice versa.
I hope this helps someone out.
Vadtec