Forum Moderators: coopster
Basically I want to get the info posted in a text field and remove certain strings and replace them with my own strings and store in database.
for example I want to put <p> instead of /r/n and replace [B] with <strong> [url] with <a href= etc.
I have been using str_replace to do this - quite lengthy, so then I was looking at using arrays.
So i'm going to use
$NameOfArray=array('<', '>', '\\', '/', '=');
$text=str_replace($NameOfArray, "", $text);
I also want to reverse the process for easy viewing when someone edits the textara.
just wondering what the best function to use is or whether there is a really cool way that I have overlooked?
I´m not really sure whether deleting <> is such a good idea. People might like to use them as literal characters. Use htmlspecialchars to encode those characters when you display them on a page.
Andreas
$DataForm=array('<strong>', '</strong>', '<p>', '<br>', '<li>', '</li>');
$FormData=array('*b*', '*/b*', '\r\n', '\n', '[list]', '[/list]');
//to add to the DB
$text=str_replace($FormData, $DataForm, $text);
//to display in the textarea
$text=str_replace($DataForm, $FormData, $text);
which seems to work OK but Im having problems with the linebreaks it keeps adding them and also get \ in the text....is there a special way to encode these characters?
There´s quite a bit info about linebreaks in the thread I linked to.
\r\n => <p> and \n => <br> probably does not achieve what you want. See the other thread.
Andreas
// find $( xx )
function DoSubst ($Text, $NVPArray)
{
$NewStr = "";
$l = strlen ($Text);
$i = 0;
$c = 0;
while ($i < $l) {
if ($c++ > 100) break;
$x = strpos ($Text, "$(", $i);
if ($x === false) {
$NewStr .= substr ($Text, $i);
$i = $l;
} else {
$NewStr .= substr ($Text, $i, $x - $i);
$i += $x;
$Tmp = substr ($Text, $x + 2);
$y = strpos ($Tmp, ")") + $x + 2;
if ($y - $x <= 2) {
$NewStr .= substr ($Text, $x);
$i = $l;
} else {
$TagName = substr ($Text, $x + 2, $y - $x - 2);
$TagValue = urlencode ($NVPArray [$TagName]);
$NewStr .= $TagValue;
$i = $y + 1;
}
}
}
return ($NewStr);
}
SO, a sample call might look like:
$textthing = "blah$(var1)blah$(var2)blahblah";
$myarray ["var1"] = ". ";
$myarray ["var2"] = " no blah ";
echo DoSubst ($textthing, $myarray);