/    Sign up×
Community /Pin to ProfileBookmark

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?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@curtayauthorMar 05.2010 — [code=php]str_ireplace("&#194;", "", $text) [/code] has no effect
Copy linkTweet thisAlerts:
@curtayauthorMar 05.2010 — 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
Copy linkTweet thisAlerts:
@MindzaiMar 05.2010 — I tend to use the following sort of thing:

[code=php]
function slug($string, $replacement = '-') {
$map = array(
'/&#224;|&#225;|&#229;|&#226;/' => 'a',
'/&#232;|&#233;|&#234;|&#7869;|&#235;/' => 'e',
'/&#236;|&#237;|&#238;/' => 'i',
'/&#242;|&#243;|&#244;|&#248;/' => 'o',
'/&#249;|&#250;|&#367;|&#251;/' => 'u',
'/&#231;/' => 'c',
'/&#241;/' => 'n',
'/&#228;|&#230;/' => 'ae',
'/&#246;/' => 'oe',
'/&#252;/' => 'ue',
'/&#196;/' => 'Ae',
'/&#220;/' => 'Ue',
'/&#214;/' => 'Oe',
'/&#223;/' => 'ss',
'/[^ws]/' => '',
'/\s+/' => $replacement,
"/$replacement+/" => $replacement
);
return preg_replace(array_keys($map), array_values($map), $string);
}[/code]


Obviously you would adjust the $map array according to your needs.
Copy linkTweet thisAlerts:
@curtayauthorMar 07.2010 — 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: '/[^ws]/' => '',

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?
Copy linkTweet thisAlerts:
@Jarrod1937Mar 07.2010 — 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.
Copy linkTweet thisAlerts:
@MindzaiMar 07.2010 — 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.

[code=php]$str = 'foo&#194;bar';
echo str_replace('&#194;', '', $str); // foobar[/code]
Copy linkTweet thisAlerts:
@MindzaiMar 07.2010 — 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.[/QUOTE]

There are lots of reasons, for example creating valid URLs.
Copy linkTweet thisAlerts:
@Jarrod1937Mar 07.2010 — There are lots of reasons, for example creating valid URLs.[/QUOTE]
Oh, didn't think about url's... that would be a problem.
Copy linkTweet thisAlerts:
@Riana90Aug 18.2011 — 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.
Copy linkTweet thisAlerts:
@NogDogAug 18.2011 — ...

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.[/QUOTE]


Only if it is going to be used in a URL, in which case you can use urlencode() when you actually output it.
Copy linkTweet thisAlerts:
@BellapattinsAug 19.2011 — 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..
Copy linkTweet thisAlerts:
@NogDogAug 19.2011 — 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..[/QUOTE]


Maybe output the page as UTF-8 character encoding, and then don't worry about it?
×

Success!

Help @curtay spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...