removing strange characters from php string

前端 未结 14 925
忘了有多久
忘了有多久 2021-01-31 17:45

this is what i have right now

Drawing an RSS feed into the php, the raw xml from the rss feed reads:

Paul’s Confidence

The ph

相关标签:
14条回答
  • 2021-01-31 18:28

    This is my function that always works, regardless of encoding:

    function RemoveBS($Str) {  
      $StrArr = str_split($Str); $NewStr = '';
      foreach ($StrArr as $Char) {    
        $CharNo = ord($Char);
        if ($CharNo == 163) { $NewStr .= $Char; continue; } // keep £ 
        if ($CharNo > 31 && $CharNo < 127) {
          $NewStr .= $Char;    
        }
      }  
      return $NewStr;
    }
    

    How it works:

    echo RemoveBS('Hello õhowå åare youÆ?'); // Hello how are you?
    
    0 讨论(0)
  • 2021-01-31 18:28

    This will remove all non-ascii characters / special characters from a string.

    //Remove from a single line string
    $output = "Likening ‘not-critical’ with";
    $output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
    echo $output;
     
    //Remove from a multi-line string
    $output = "Likening ‘not-critical’ with \n Likening ‘not-critical’ with \r Likening ‘not-critical’ with. ' ! -.";
    $output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $output);
    echo $output;

    0 讨论(0)
  • 2021-01-31 18:29

    Many Strange Character be removed by applying mysqli_set_charset($con,"utf8"); below the mysql connection code.

    but in some circumstances of removing this type strange character like â€

    we need to use: $title = ' Stefen Suraj'; $newtitle = preg_replace('/[^(\x20-\x7F)]*/','', $title); echo $newtitle;

    Output will be: Stefen Suraj

    0 讨论(0)
  • 2021-01-31 18:29

    Just one simple solution.

    if your string contains these type of strange chars suppose $text contains some of these then just do as shown bellow:

    $mytext=mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8')
    

    and it will work..

    0 讨论(0)
  • 2021-01-31 18:32

    Yeah this is not working for me. What is the workaround for this? – vaichidrewar Mar 12 at 22:29

    Add this to the HTML head (or modify if already there):

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    

    This will encode the funny chars like "“" into UTF-8 so that the str_replace() function will interpret them properly.

    Or you can do this:

    ini_set('default_charset', 'utf-8');
    
    0 讨论(0)
  • 2021-01-31 18:32

    Is the character encoding setting for your PHP server something other than UTF-8? If so, is there a reason or could it be changed to UTF-8? Though we don't store data in UTF-8 in our database, I've found that setting the webserver's character set to UTF-8 seems to help resolve character set issues.

    I'd be interested in hearing others' opinions about this... whether I'm setting myself up for problems by setting webserver to UTF-8 while storing submitted data in Latin1 in our mysql database. I know there was a reason I chose Latin1 for the database but can't recall what it was. Interestingly, our current setup seems to allow for non-UTF-8 character entry and subsequent rendering... it seems that storing in Latin1 doesn't prevent subsequent decoding and display of all UTF-8 characters?

    0 讨论(0)
提交回复
热议问题