Here is an example for you.
First make an array like this:
$translate_array= array(
' & ' => ' & ',
'"&"' => '"&"',
'W&OD' => 'W&OD',
'&O' => '&O',
'M&F' => 'M&F',
);
Then use:
$find_array= array_keys($translate_array);
$replace_array= array_values($translate_array);
$text= preg_replace($find_array, $replace_array, $text);
//with preg_replace you can make the $find stuff case insensitive
OR simply:
$text= strtr($text, $translate_array);
J_K9 wrote:
Hi,
I'm trying to code a PHP app to convert my inputted HTML code (into a
textarea) into BBCode, for use on a forum. I have tried to code it,
but have had little success so far. Here is the code I wrote (sorry,
I'm still learning):
-------CODE-------
<html>
<head>
<title>Convert from HTML to BBCode</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Body: <br /><textarea name="text"></textarea><br /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
<?php
$text = $_REQUEST['text'];
echo '<br /><br />';
// Declare HTML tags to find, and BBCode tags to replace them with
$linkStartFind = '<a href="';
$linkStartReplace = '[url=';
$linkEndFind = '</a>';
$linkEndReplace = '[/url]';
$italicStartFind = '<em>';
$italicStartReplace = '[i]';
$italicEndFind = '</em>';
$italicEndReplace = '[/i]';
$boldStartFind = '<strong>';
$boldStartReplace = '[b]';
$boldEndFind = '</strong>';
$boldEndReplace = '[/b]';
$imgStartFind = '<img src="';
$imgStartReplace = '[img]';
$imgEndFind = ' border="0" />';
$imgEndReplace = '[/img]';
$OLBeginFind = '<ol>';
$OLBeginReplace = '';
$OLFinishFind = '</ol>';
$OLFinishReplace = '';
$listStartFind = '<li>';
$listStartReplace = '[list]';
$listEndFind = '</li>';
$listEndReplace = '[/list]';
// Replace.
$text = str_replace($linkStartFind, $linkStartReplace, $text);
$text = str_replace($linkEndFind, $linkEndReplace, $text);
$text = str_replace($italicStartFind, $italicStartReplace, $text);
$text = str_replace($italicEndFind, $italicEndReplace, $text);
$text = str_replace($boldStartFind, $boldEndReplace, $text);
$text = str_replace($boldEndFind, $boldEndReplace, $text);
$text = str_replace($imgStartFind, $imgStartReplace, $text);
$text = str_replace($imgEndFind, $imgEndReplace, $text);
$text = str_replace($OLStartFind, $OLStartReplace, $text);
$text = str_replace($OLEndFind, $OLEndReplace, $text);
$text = str_replace($listStartFind, $listStartReplace, $text);
$text = str_replace($listEndFind, $listEndReplace, $text);
echo '<textarea name="output">' . "$text" . '</textarea>';
?>
</body>
</html>
-------/CODE-------
Now, most of this doesn't work. Here is the test code I put into the
first textarea:
-------TESTCODE-------
<strong>Testing bold code</strong>
<em>Testing italics</em>
<a href="http://link.com">Testing link</a>
<img src="http://image.com/img.jpg" border="0" />
<img src="http://image.com/img2.jpg" style="padding-right: 5px;"
border="0" />
-------/TESTCODE-------
And here's what I got out:
-------RESULT-------
[/b]Testing bold code[/b]
[i]Testing italics[/i]
<a href=\"http://link.com\">Testing link[/url]
<img src=\"http://image.com/img.jpg\" border=\"0\" />
<img src=\"http://image.com/img2.jpg\" style=\"padding-right: 5px;\"
border=\"0\" />
-------/RESULT-------
As you can see, the bold, italic, and ending hyperlink tag
replacements worked, but the rest didn't. Backslashes have been added
where there are "", and if there were anything between an img
tag's 'src="{image}"' and ' border="0" />' that wouldn't be removed,
and therefore provide me with a faulty link.
Just to clarify the BBCode tags, they are:
[url=http://link.com]Click this link[/url]
[img]http://imagesite.com/image.jpg[/img]
[b]_BOLD_[/b]
[i]italicised[/i]
[u]underlined[/i]
I would really like to get this working, as it'll not only help me
improve my PHP skills but also aid my tutorial conversions - it takes
ages to do this by hand ;)
Any help would be appreciated. Thanks in advance,
J_K9
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php