Hi,
Thank you for that - although I do have to admit that I don't
understand a single bit of the reg-exp part. :)
It is now working flawlessly! Here is the final code I used:
-------CODE-------
<!-- Note - input textarea and submit button already coded here in
XHTML -->
<?php
$text = '';
if ( isset($_POST['text']) ) {
$text = $_POST['text'];
if ( get_magic_quotes_gpc() ) {
$text = $_POST['text'];
$text = stripslashes($text);
}
}
echo '<br /><br />';
/**
* Performes BBCode conversion for some simple HTML elements
*
* @staticvar string $str_http_valid
* @staticvar array $arr_replace
* @param string $string
* @return string
* @since Mon Mar 06 23:44:40 CST 2006
* @author rsalazar
*/
function to_bbcode( $string ) {
static $str_http_valid = '-:\/a-z.0-9_%+';
$arr_replace = array(
"/<a\s+.*?(?<=\b)href=(?(?=['\"])(?:(['\"])(.*?)\\1)|([$str_http_valid]
*)).*?>(.+?)<\/a>/Xis"
=> '[link=\\2\\3]\\4[/link]',
"/<img\s+.*?(?<=\b)src=(?(?=['\"])(?:(['\"])(.*?)\\1)|
([$str_http_valid]*)).*?\/?>/Xis"
=> '[img]\\2\\3[/img]',
'/<(\/)?(strong|em)>/Xise' => '( strcasecmp("em", "\\2") ?
"[\\1b]" : "[\\1i]" )',
'/<(\/?(?:b|i|u))>/Xis' => '[\\1]',
'/<(\/)?[ou]l>/Xis' => '[\\1list]',
'/<(\/)?li>/Xise' => '( "\\1" == "" ? "[*]" : "" )',
);
$string = preg_replace(array_keys($arr_replace),
array_values($arr_replace),
$string);
return $string;
}
$text = to_bbcode($text);
echo '<textarea name="output">' . "$text" . '</textarea>';
?>
-------/CODE-------
Thank you very much for your help! I hope I get onto reg-exp and PREG
stuff soon so that someday I might be able to understand that code.. :D
Cheers,
J_K9
> First of all, the back-slashes added before a " character is
probably
> because of the gpc_magic_quotes directive in PHP, wich tries
to "escape"
> the quotes (pretty stupid, if you ask me), so you must have to use
> strip_slashes() on the string you received, e.g:
> $text = '';
> if ( isset($_POST['text']) ) {
> $text = $_POST['text'];
> if ( get_magic_quotes_gpc() ) {
> $text = stripslashes($text);
> }
> }
>
> Now, what you're trying to do is definetely not
something "basic",
> since you want to replace some non-fixed strings that can either be
in
> lower or uppercase (and without changing the case of the rest of the
> text), so basicaly what you have are patterns (some kind of 'rules'
that
> shall be followed by the tags)
>
> By your code I can tell you've already try a little the hard
way to
> solve this issue, although it would be quite more laborious than
that
> because you would have to search the string almost char-by-char (in
a
> figurative way, but pretty much what PHP would be doing) for all the
> tags you want to replace, and possibly be working with two strings:
one
> for the original text and other with a lowercase version of it
(since
> you cannot search in a case-insensitive way --only in PHP5)
>
> Anyway, the medium/advanced way (IMHO) would be to use regular
> expressions. These are quite useful, but also rather cryptic, even
for
> advanced users --sometimes it's easier to come up with a new one
rather
> than understanding what already exists :p
>
> The function I've test with your test HTML-code is this one:
> /**
> * Performes BBCode conversion for some simple HTML elements
> *
> * @staticvar string $str_http_valid
> * @staticvar array $arr_replace
> * @param string $string
> * @return string
> * @since Mon Mar 06 23:44:40 CST 2006
> * @author rsalazar
> */
> function to_bbcode( $string ) {
> static $str_http_valid = '-:\/a-z.0-9_%+';
> $arr_replace = array(
>
> "/<a\s+.*?(?<=\b)href=(?(?=['\"])(?:(['\"])(.*?)\\1)|
([$str_http_valid]*)).*?>(.+?)<\/a>/Xis"
> => '[link=\\2\\3]\\4[/link]',
>
> "/<img\s+.*?(?<=\b)src=(?(?=['\"])(?:(['\"])(.*?)\\1)|
([$str_http_valid]*)).*?\/?>/Xis"
> => '[img]\\2\\3[/img]',
> '/<(\/)?(strong|em)>/Xise' => '( strcasecmp
("em", "\\2") ?
> "[\\1b]" : "[\\1i]" )',
> '/<(\/?(?:b|i|u))>/Xis' => '[\\1]',
>
> '/<(\/)?[ou]l>/Xis' => '[\\1list]',
> '/<(\/)?li>/Xise' => '( "\\1" == "" ? "[*]" : "" )',
> );
> $string = preg_replace(array_keys($arr_replace),
> array_values($arr_replace),
> $string);
> return $string;
> }
>
> As I mentiones before, keep in mind that reg-exp can be rather
cryptic
> sometimes. Also, this is the raw code, it should be optimized but
I'm
> feeling really lazy right now, so it should have to wait for a
better
> ocasion.
>
> It's up to you to decide wheter you'll use this function or
not, what I
> would recommend you is not to forget about regexp and give them a
try
> later (when you're more familiar with PHP), and I would also
recommend
> you to use PREG family rather than EGREP.
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php