www.webdeveloper.com
Results 1 to 13 of 13

Thread: How to remove weird characters from string?

  1. #1
    Join Date
    Mar 2008
    Posts
    70

    Question How to remove weird characters from string?

    Hi, I have a string like this: <br /> <br /> <br /> <br /> ...

    I want to remove the . I want to keep most other symbols.

    $text = str_ireplace(array("&#194;",""), "", $text) Has no effect. How do I remove them?

  2. #2
    Join Date
    Mar 2008
    Posts
    70
    PHP Code:
    str_ireplace("Â"""$text
    has no effect

  3. #3
    Join Date
    Mar 2008
    Posts
    70
    wow the forum won't display my html entity codes even though the code is in PHP forum tags.
    str_ireplace("ampersand pound 194;", "", $text) has no effect

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    I tend to use the following sort of thing:

    PHP Code:
    function slug($string$replacement '-') {
        
    $map = array(
            
    '/à|á|å|â/' => 'a',
            
    '/è|é|ê|ẽ|ë/' => 'e',
            
    '/ì|í|î/' => 'i',
            
    '/ò|ó|ô|ø/' => 'o',
            
    '/ù|ú|ů|û/' => 'u',
            
    '/ç/' => 'c',
            
    '/ñ/' => 'n',
            
    '/ä|æ/' => 'ae',
            
    '/ö/' => 'oe',
            
    '/ü/' => 'ue',
            
    '/Ä/' => 'Ae',
            
    '/Ü/' => 'Ue',
            
    '/Ö/' => 'Oe',
            
    '/ß/' => 'ss',
            
    '/[^\w\s]/' => '',
            
    '/\\s+/' => $replacement,
            
    "/$replacement+/" => $replacement
        
    );
        return 
    preg_replace(array_keys($map), array_values($map), $string);

    Obviously you would adjust the $map array according to your needs.
    The first rule of Tautology Club is the first rule of Tautology Club.

  5. #5
    Join Date
    Mar 2008
    Posts
    70
    Hi that is converting most of the symbols such as the pound # symbol and I don't want it to.

    This part of the map: '/[^\w\s]/' => '',
    is the only part that is converting the &#194;'s and that's good but it's also converting most other symbols and I don't want it to. Any suggestions?

  6. #6
    Join Date
    Mar 2010
    Posts
    672
    Why do you want them translated to pure ascii? Only reason i can think of is your character encoding decoding setup is messed up and foreign characters are being outputted as garbage. If you use correct character encoding you should be fine, you'll just have input/output with funny looking A's if the user inputted such characters.
    Last edited by Jarrod1937; 03-06-2010 at 06:37 PM.

  7. #7
    Join Date
    Nov 2008
    Posts
    2,477
    You literally just want &#194; removed? The following works fine for me, just remember to save your php file as UTF-8 with no BOM.

    PHP Code:
    $str 'fooÂbar';
    echo 
    str_replace('Â'''$str); // foobar 
    The first rule of Tautology Club is the first rule of Tautology Club.

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by Jarrod1937 View Post
    Why do you want them translated to pure ascii? Only reason i can think of is your character encoding decoding setup is messed up and foreign characters are being outputted as garbage. If you use correct character encoding you should be fine, you'll just have input/output with funny looking A's if the user inputted such characters.
    There are lots of reasons, for example creating valid URLs.
    The first rule of Tautology Club is the first rule of Tautology Club.

  9. #9
    Join Date
    Mar 2010
    Posts
    672
    Quote Originally Posted by Mindzai View Post
    There are lots of reasons, for example creating valid URLs.
    Oh, didn't think about url's... that would be a problem.

  10. #10
    Join Date
    Aug 2011
    Posts
    1
    Hi there!!
    I've noticed that when webform.module receives a file upload containing a space in the filename, it emails a link with that space included in the URL instead of escaped as + or %20. For example:

    Submitted values are:
    File: http://mysite.com/files/webform/barrytest/Barry Test.jpg

    I think the problem lies in file_create_url(). It says:

    <?php
    switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
    case FILE_DOWNLOADS_PUBLIC:
    return $GLOBALS['base_url'] .'/'. file_directory_path() .'/'. str_replace('\\', '/', $path);
    ?>

    Shouldn't $path be urlencode()d there? The patch is trivial but I do not have time at the moment to check to see whether it will break anything.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by Riana90 View Post
    ...
    Shouldn't $path be urlencode()d there? The patch is trivial but I do not have time at the moment to check to see whether it will break anything.
    Only if it is going to be used in a URL, in which case you can use urlencode() when you actually output it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Aug 2011
    Posts
    1
    please tell me how to remove ï » ¿ â ¢ ® and all such characters from a string.
    there are many such characters i havent written here.

    i think it is very difficult to list them all in an array and to use str_replace function etc..

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by Bellapattins View Post
    please tell me how to remove ï » ¿ â ¢ ® and all such characters from a string.
    there are many such characters i havent written here.

    i think it is very difficult to list them all in an array and to use str_replace function etc..
    Maybe output the page as UTF-8 character encoding, and then don't worry about it?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles