Jump to content

Remove special charectors from variable


bimaljr

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.