bimaljr Posted May 18, 2009 Share Posted May 18, 2009 here is my code : <?php $data = "This is “test” this is normal text."; print $data; $data2 = str_replace('“','_',$data); $data2 = str_replace('”','_',$data2); print ' <br> '; print $data2; print '<hr>'; // Database connection mysql_connect("localhost", "root", "test") or die(mysql_error()); mysql_select_db("mydb") or die(mysql_error()); // getting menu list $mainmenuquery = "SELECT * FROM datatable WHERE id=100"; $mainmenuresult = mysql_query($mainmenuquery) or die(mysql_error()); while($mainmenurow = mysql_fetch_array($mainmenuresult, MYSQL_ASSOC)) { //print $mainmenurow['bodytext']; $introtext = str_replace('”','_',$mainmenurow['bodytext']); $introtext = str_replace('“','_',$introtext); //$introtext = str_replace('test','_',$introtext); // This is to test that this is working or not. print ' <br> '; print $introtext; print '<hr>'; } ?> The first code before <hr> tag is working and the str_replace is also working perfectly. But when I fatch the same data from database and apply the str_replace command than it does not work. My "datatable" table contains the the same test as $data variable contains. But still not working. Please help me with this. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/158597-remove-special-charectors-from-variable/ Share on other sites More sharing options...
premiso Posted May 18, 2009 Share Posted May 18, 2009 if (mysql_num_rows($mainmenuresult) > 0) { while($mainmenurow = mysql_fetch_array($mainmenuresult, MYSQL_ASSOC)) { //print $mainmenurow['bodytext']; $introtext = str_replace('”','_',$mainmenurow['bodytext']); $introtext = str_replace('“','_',$introtext); //$introtext = str_replace('test','_',$introtext); // This is to test that this is working or not. print ' <br> '; print $introtext; print '<hr>'; } }else { print 'The database did not return any rows'; } See if that displays the "The database did not return any rows". And please, if it does not define how it is "not working" Is it returning data and just not replacing the data? Or is it not returning data at all? Quote Link to comment https://forums.phpfreaks.com/topic/158597-remove-special-charectors-from-variable/#findComment-836489 Share on other sites More sharing options...
bimaljr Posted May 19, 2009 Author Share Posted May 19, 2009 if it does not define how it is "not working" Is it returning data and just not replacing the data? Or is it not returning data at all? I am getting data of Mysql table perfectly. But the str_replace() function is not working for only special characters for mysql results only. Example: This is my datatable table. |----|-------------------------------------| |id | bodytext | |----|-------------------------------------| |100 | This is “test” this is normal text. | |101 | This is “test” this is normal text. | |102 | This is “test” this is normal text. | |----|-------------------------------------| Here is my code : // Database connection mysql_connect("localhost", "root", "test") or die(mysql_error()); mysql_select_db("mydb") or die(mysql_error()); // getting menu list $mainmenuquery = "SELECT * FROM datatable WHERE id=100"; $mainmenuresult = mysql_query($mainmenuquery) or die(mysql_error()); while($mainmenurow = mysql_fetch_array($mainmenuresult, MYSQL_ASSOC)) { $introtext = str_replace('”','_',$mainmenurow['bodytext']); // this is not working $introtext = str_replace('“','_',$introtext); // this is also not working print $introtext; The output of above is perfect but the str_replace() function is not working at all. I am getting this : This is “test” this is normal text But I want to get this : This is _test_ this is normal text Quote Link to comment https://forums.phpfreaks.com/topic/158597-remove-special-charectors-from-variable/#findComment-837179 Share on other sites More sharing options...
premiso Posted May 19, 2009 Share Posted May 19, 2009 Try using the index of the char for those characters. As saving the characters are probably getting saved in your text file as non-smart quotes. Which is causing the issue. Quote Link to comment https://forums.phpfreaks.com/topic/158597-remove-special-charectors-from-variable/#findComment-837263 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.