lalnfl Posted November 25, 2010 Share Posted November 25, 2010 How come this is not replacing correctly? $rp = strtr($rp,"[TIMES]","<span style='font-family: Times New Roman, Times, serif'>") Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/ Share on other sites More sharing options...
Zyx Posted November 25, 2010 Share Posted November 25, 2010 PHP manual is your friend: http://docs.php.net/strtr - strtr() replaces single characters, not substrings. I guess you want to use str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139398 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2010 Share Posted November 25, 2010 You probably should be using str_replace instead: <?php $rp = str_replace('[TIMES]',"<span style='font-family: Times New Roman, Times, serif'>",$rp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139399 Share on other sites More sharing options...
lalnfl Posted November 25, 2010 Author Share Posted November 25, 2010 How do I get str_replace to not match characters or parts of words? Because when I get the end result I come up with <TiMES> as my result. I tried putting in [a] in for [TIMES] and it came out as <a> as my result. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139400 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2010 Share Posted November 25, 2010 What is the initial value of the variable $rp? This works fine: <?php $rp = '[TIMES]'; $rp = str_replace('[TIMES]',"<span style='font-family: Times New Roman, Times, serif'>",$rp); echo $rp; ?> It echos <span style='font-family: Times New Roman, Times, serif'> Ken Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139401 Share on other sites More sharing options...
lalnfl Posted November 25, 2010 Author Share Posted November 25, 2010 What is the initial value of the variable $rp? Ken It is a textarea field. And I tested it again with search [TIME] as my replace <span style='color: blue'> and it still comes out with the result <TiME> Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139403 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2010 Share Posted November 25, 2010 Please post the code you're using. Ken Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139404 Share on other sites More sharing options...
lalnfl Posted November 25, 2010 Author Share Posted November 25, 2010 <select id='font_text' class='text4' onchange='changeFont()'> <option value='0'>FONT</option> <option value='ARIAL'>Arial</option> <option value='TIMES'>Times</option> <option value='COURIER'>Courier</option> <option value='GEORGIA'>Georgia</option> <option value='VERDANA'>Verdana</option> <option value='GENEVA'>Geneva</option> </select> <textarea class='text4' cols='100' rows='15' name='rp' id='rp_area'>$rp</textarea> Javascript: function changeFont(){ if (document.getElementById('font_text').selected=true){ var v = document.getElementById('font_text').value; var ls = "["; var rs = "]"; var s = "/"; var y = document.getElementById('rp_area').value; var x = document.getElementById('rp_area').innerHTML=ls+v+rs; var z = document.getElementById('rp_area').innerHTML=ls+s+v+rs; var xz = x+z; var xy = y+xz; document.getElementById('rp_area').focus(); document.getElementById('rp_area').value=xy; document.getElementById('font_text').value=0; } } Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139406 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2010 Share Posted November 25, 2010 And where is your PHP code? Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139407 Share on other sites More sharing options...
lalnfl Posted November 25, 2010 Author Share Posted November 25, 2010 Sorry I posted the wrong thing. if (isset ($_POST['rp'])){ // define variables $rp = $_POST['rp']; $rp_event = $_POST['rp_event']; $rp_title = $_POST['rp_title']; $rp_preview = $_POST['rp_preview']; $rp_category1 = $_POST['rp_category1']; $rp_category2 = $_POST['rp_category2']; $rp_category3 = $_POST['rp_category3']; $rp_global = $_POST['rp_global']; // error handling $rp = strip_tags($rp); $rp_title = strip_tags($rp_title); $rp_preview = strip_tags($rp_preview); $rp = stripslashes($rp); $rp_title = stripslashes($rp_title); $rp_preview = stripslashes($rp_preview); $rp = htmlspecialchars($rp); $rp_title = htmlspecialchars($rp_title); $rp_preview = htmlspecialchars($rp_preview); $rp = strtr($rp,"`",""); $rp_title = strtr($rp_title,"`",""); $rp_preview = strtr($rp_preview,"`",""); // current number of RP's for event $sql_get_counter = mysql_query("SELECT * FROM Worker_RP WHERE worker_id='$worker_id' AND promo_id='$promo_id' AND event_id='$rp_event'"); $counter = mysql_num_rows($sql_get_counter); // error messages if ((!$rp_event) || (!$rp_title) || (!$rp) || (!$rp_category1) || (!$rp_global)){ $errormsg = "<p class='text3' style='font-weight: bold; color: #ff0000;'>You did not fill in the following fields</p>"; if (!$rp_event){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>Roleplay Event</p>"; } if (!$rp_title){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>Roleplay Title</p>"; } if (!$rp){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>Roleplay</p>"; } if (!$rp_category1){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>Roleplay Category</p>"; } if (!$rp_global){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>Roleplay Global Grade</p>"; } } else if (str_word_count($rp) > $word_limit){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You went over the word limit!</p>"; } else if (strlen($rp_preview) > 500){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You went over the maximum 500 character count for your roleplay preview!</p>"; } else if ($counter >= $rp_limit){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You cannot post more than $rp_limit roleplays!</p>"; } else if (substr_count($rp, "[b]") != substr_count($rp, "[/b]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open bold tags!</p>"; } else if (substr_count($rp, "[i]") != substr_count($rp, "[/i]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open italic tags!</p>"; } else if (substr_count($rp, "[u]") != substr_count($rp, "[/u]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open underline tags!</p>"; } else if (substr_count($rp, "[ARIAL]") != substr_count($rp, "[/ARIAL]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open arial tags!</p>"; } else if (substr_count($rp, "[TIMES]") != substr_count($rp, "[/TIMES]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open times tags!</p>"; } else if (substr_count($rp, "[COURIER]") != substr_count($rp, "[/COURIER]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open courier tags!</p>"; } else if (substr_count($rp, "[GEORGIA]") != substr_count($rp, "[/GEORGIA]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open georgia tags!</p>"; } else if (substr_count($rp, "[VERDANA]") != substr_count($rp, "[/VERDANA]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open verdana tags!</p>"; } else if (substr_count($rp, "[GENEVA]") != substr_count($rp, "[/GENEVA]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open geneva tags!</p>"; } else if (substr_count($rp, "[sMALL]") != substr_count($rp, "[/sMALL]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open small tags!</p>"; } else if (substr_count($rp, "[NORMAL]") != substr_count($rp, "[/NORMAL]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open normal tags!</p>"; } else if (substr_count($rp, "[LARGE]") != substr_count($rp, "[/LARGE]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open large tags!</p>"; } else if (substr_count($rp, "[bLUE]") != substr_count($rp, "[/bLUE]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open blue tags!</p>"; } else if (substr_count($rp, "[RED]") != substr_count($rp, "[/RED]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open red tags!</p>"; } else if (substr_count($rp, "[GREEN]") != substr_count($rp, "[/GREEN]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open green tags!</p>"; } else if (substr_count($rp, "[YELLOW]") != substr_count($rp, "[/YELLOW]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open yellow tags!</p>"; } else if (substr_count($rp, "[ORANGE]") != substr_count($rp, "[/ORANGE]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open orange tags!</p>"; } else if (substr_count($rp, "[GRAY]") != substr_count($rp, "[/GRAY]")){ $errormsg .= "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have open gray tags!</p>"; } else { // error messages are done $rp = mysql_real_escape_string($rp); $rp_title = mysql_real_escape_string($rp_title); $rp_preview = mysql_real_escape_string($rp_preview); $rp = strtr($rp,"`",""); $rp_title = strtr($rp_title,"`",""); $rp_preview = strtr($rp_preview,"`",""); $rp = strtr($rp,"[b]","<b>"); $rp = strtr($rp,"[/b]","</b>"); $rp = strtr($rp, "[i]","<i>"); $rp = strtr($rp,"[/i]","</i>"); $rp = strtr($rp,"[u]","<u>"); $rp = strtr($rp,"[/u]","</u>"); $rp = str_replace('[TIME]',"<span style='color: blue'>",$rp); /* $rp = strtr($rp,"[TIMES]","<span style='font-family: Times New Roman, Times, serif'>"); $rp = strtr($rp,"[/TIMES]","</span>"); $rp = strtr($rp,"[COURIER]","<span style='font-family: Courier New, Courier, mono'>"); $rp = strtr($rp,"[/COURIER]","</span>"); $rp = strtr($rp,"[GEORGIA]","<span style='font-family: Georgia, Times New Roman, Times, serif'>"); $rp = strtr($rp,"[/GEORGIA]","</span>"); $rp = strtr($rp,"[VERDANA]","<span style='font-family: Verdana, Arial, Helvetica, sans-serif'>"); $rp = strtr($rp,"[/VERDANA]","</span>"); $rp = strtr($rp,"[GENEVA]","<span style='font-family: Geneva, Arial, Helvetica, sans-serif'>"); $rp = strtr($rp,"[/GENEVA]","</span>"); $rp = strtr($rp,"[sMALL]","<span style='font-size: 11px'>"); $rp = strtr($rp,"[/sMALL]","</span>"); $rp = strtr($rp,"[NORMAL]","<span style='font-size: 12px'>"); $rp = strtr($rp,"[/NORMAL]","</span>"); $rp = strtr($rp,"[LARGE]","<span style='font-size: 14px'>"); $rp = strtr($rp,"[/LARGE]","</span>"); $rp = strtr($rp,"[color=blue]","<span style='color: #0000ff'>"); $rp = strtr($rp,"[/color]","</span>"); $rp = strtr($rp,"[RED]","<span style='color: #ff0000'>"); $rp = strtr($rp,"[/RED]","</span>"); $rp = strtr($rp,"[GREEN]","<span style='color: #00ff00'>"); $rp = strtr($rp,"[/GREEN]","</span>"); $rp = strtr($rp,"[YELLOW]","<span style='color: #ffff00'>"); $rp = strtr($rp,"[/YELLOW]","</span>"); $rp = strtr($rp,"[ORANGE]","<span style='color: #ffa500'>"); $rp = strtr($rp,"[/ORANGE]","</span>"); $rp = strtr($rp,"[GRAY]","<span style='color: #c0c0c0'>"); $rp = strtr($rp,"[/GRAY]","</span>"); */ $rp_replace = array("<b>" => "","</b>" => "","<i>" => "","</i>" => "","<u>" => "","</u>" => "","<span style='font-family: Arial, Helvetica, sans-serif;'>" => "","<span style='font-family: 'Times New Roman', Times, serif;'>" => "","<span style='font-family: 'Courier New', Courier, mono;'>" => "","<span style='font-family: Georgia, 'Times New Roman', Times, serif;'>" => "","<span style='font-family: Verdana, Arial, Helvetica, sans-serif;'>" => "","<span style='font-family: Geneva, Arial, Helvetica, sans-serif;'>" => "","<span style='font-size: 11px;'>" => "","<span style='font-size: 12px;'>" => "","<span style='font-size: 14px;'>" => "","<span style='color: #0000ff;'>" => "","<span style='color: #ff0000;'>" => "","<span style='color: #00ff00;'>" => "","<span style='color: #ffff00;'>" => "","<span style='color: #ffa500;'>" => "","<span style='color: #c0c0c0;'>" => "","</span>" => ""); $new_rp = strtr($rp, $rp_replace); $rp_word_count = str_word_count($new_rp); if ($rp_preview == ""){ $rp_preview = substr($rp, 0, 500); } $sql_insert_rp = mysql_query("INSERT INTO Worker_RP (worker_id, promo_id, event_id, category_id1, category_id2, category_id3, words, time_posted, date_posted, preview, content, title, allow_global_grade, posted) VALUES ('$worker_id' , '$promo_id' , '$rp_event' , '$rp_category1' , '$rp_category2' , '$rp_category3' , '$rp_word_count' , CURTIME() , CURDATE() , '$rp_preview' , '$rp' , '$rp_title' , '$rp_global' , now())") or die (mysql_error()); $errormsg = "<p class='text3' style='font-weight: bold; color: #ff0000;'>You have successfully submitted your Roleplay!</p>"; // clear variables $rp = ""; $rp_event = ""; $rp_title = ""; $rp_preview = ""; $rp_category1 = ""; $rp_category2 = ""; $rp_category3 = ""; $rp_global = ""; } } else { $errormsg = "<p class='text3' style='font-weight: bold; color: #ff0000;'>All of the fields are required except for you only need one Roleplay Category!</p>"; } $content = " $errormsg <form action='fed_roleplays.php?promotion_id=$promo_id&create_rp' method='post'> <p class='text3'>Roleplaying as <a href='workers.php?worker_id=$worker_id' style='font-weight: bold'>$worker_firstname $worker_lastname</a></p> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Roleplaying for</p> </div> <div style='width: 538px; float: left;'> <p class='text3'> <select name='rp_event' size='1'> <option value='$rp_event'>$rp_event</option> $upcoming_events </select> </p> </div> <div class='line'></div> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Title</p> </div> <div style='width: 538px; float: left;'> <p class='text3'> <input class='text4' type='text' name='rp_title' value='$rp_title' size='20' /> </p> </div> <div class='line'></div> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Roleplay (Word Limit: $word_limit_format)</p> </div> <div style='width: 538px; float: left;'> <p class='text3'> <input type='button' id='bold_text' class='text3' style='padding: 3px; font-weight: bold;' value='B' onclick='changeBold()' /> <input type='button' id='italic_text' class='text3' style='padding: 3px; font-weight: bold;' value='I' onclick='changeItalic()' /> <input type='button' id='underline_text' class='text3' style='padding: 3px; font-weight: bold;' value='U' onclick='changeUnderline()' /> <select id='font_text' class='text4' onchange='changeFont()'> <option value='0'>FONT</option> <option value='ARIAL'>Arial</option> <option value='TIMES'>Times</option> <option value='COURIER'>Courier</option> <option value='GEORGIA'>Georgia</option> <option value='VERDANA'>Verdana</option> <option value='GENEVA'>Geneva</option> </select> <select id='size_text' class='text4' onchange='changeSize()'> <option value='0'>SIZE</option> <option value='SMALL'>Small</option> <option value='NORMAL'>Normal</option> <option value='LARGE'>Large</option> </select> <select id='color_text' class='text4' onchange='changeColor()'> <option value='0'>COLOR</option> <option value='BLUE' style='color: #0000ff;'>Blue</option> <option value='RED' style='color: #ff0000;'>Red</option> <option value='GREEN' style='color: #00ff00;'>Green</option> <option value='YELLOW' style='color: #ffff00;'>Yellow</option> <option value='ORANGE' style='color: #ffa500;'>Orange</option> <option value='GRAY' style='color: #c0c0c0;'>Gray</option> </select> </p> <p class='text3'> <textarea class='text4' cols='100' rows='15' name='rp' id='rp_area'>$rp</textarea> </p> </div> <div class='line'></div> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Custom Preview (Leave blank if no, max. 500 characters)</p> </div> <div style='width: 538px; float: left;'> <p class='text3'> <textarea class='text4' cols='100' rows='7' name='rp_preview'>$rp_preview</textarea> </p> </div> <div class='line'></div> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Category (min. 1, max. 3)</p> </div> <div style='width: 179px; float: left;'> <p class='text3'> <select name='rp_category1' size='1'> <option value='$rp_category1'>$rp_category1</option> <option value='1'>Action</option> </select> </p> </div> <div style='width: 179px; float: left;'> <p class='text3'> <select name='rp_category2' size='1'> <option value='$rp_category2'>$rp_category2</option> </select> </p> </div> <div style='width: 179px; float: left;'> <p class='text3'> <select name='rp_category3' size='1'> <option value='$rp_category3'>$rp_category3</option> </select> </p> </div> <div class='line'></div> <div style='width: 250px; float: left;'> <p class='text3' style='font-weight: bold;'>Allow global grading?</p> </div> <div style='width: 538px; float: left;'> <p class='text3' style='font-weight: bold;'> <input type='radio' name='rp_global' value='a' /> All Users </p> <p class='text3' style='font-weight: bold;'> <input type='radio' name='rp_global' value='f' /> Only Friends </p> <p class='text3' style='font-weight: bold;'> <input type='radio' name='rp_global' value='n' /> No One </p> </div> <div class='line'></div> <div style='width: 788px;' align='center'> <p class='text3'> <input type='submit' class='text3' value='Create Roleplay' /> </p> </div> </form>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139408 Share on other sites More sharing options...
lalnfl Posted November 25, 2010 Author Share Posted November 25, 2010 Help? Quote Link to comment https://forums.phpfreaks.com/topic/219786-help-with-string-replacing/#findComment-1139599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.