Trying to remove "»" with php - php

I'm trying to remove this character, and this character only from a $string with php. I've tried str_replace("»","",$test) and str_replace(chr(187),"",$test) but they can't touch it. The issue is it isn't in the same spot every time, so I can't even get creative with trimming the ends.

Are you forgetting that str_replace(old, new, string) doesn't modify the original string, but rather returns a copy of the modified string?
So:
$string = "This is the » character";
$new_string = str_replace("»", "_", $string);
echo $new_string;
Should work (it does for me)!

Wanted to point out that "»" in HTML equals » which is a standard. So, my advise would be that you better use standard characters.
Reference of characters: http://www.w3schools.com/tags/ref_entities.asp

I ended up stepping through the string one character at a time with
echo ord($test[n]
until I finally found the offending hidden character that was ruining my night.
Thanks for all the help everyone!

Related

Remove front and end white space from a string

I see this question has been asked a few times but I couldn't find an answer in php.
I have the following string.
$myString = "‍ LLC."
I run this trim($myString);
and I get this back "‍ LLC."
according to the trim documentation it should delete the white space in the front and the back? What am I missing?
I also tried htis trim($myString, " "); same results
The e2808d at the beginning of bin2hex() output is ZERO WIDTH JOINER character and the reason for trim() to not trim it. Try (PHP 7):
echo trim($myString, "\u{200d} \t\n\r\0\x0B");
I think this is happening because of the empty space is not really a real white space (tab) looks like a hidden character (â). i copy your variable and value code into an online PHP editor and i got this:

preg_match and remove multiple characters in string?

Hi I'm using php to program my site and I've been reading loads about preg_match and trying lots of examples. What I'm trying to do is something like below...
if(preg_match('These characters'), $myVariable, matches)){
Find and remove found characters from $myVariable;
}
I'm pretty sure this is obvious to php experts but it's had me stuck after hours of trying and reading.
Thanks in advance
You don't need to check for a match before doing a replace. It's like if you were to do:
str_replace("A","B","ZZZZZZZ");
It just won't replace anything. Same goes for preg_replace: If there is no match, it just does nothing.
It sounds like you should be using preg_replace. If you wanted to remove all y's and o's for example you would do this:
$string = 'hey you guys!';
$ans = preg_replace('/[yo]/','',$string);
print_r($ans); //outputs 'he u gus!'
Whatever characters you want to remove, just put them between the brackets [...]

str_replace doesn't seems to work with the implode function

After imploding an array:
$in_list = "'".implode("','",$array)."'";
$in_list content is :
'Robert','Emmanuel','José','Alexander'
Now when i try to replace the word José by another string,
str_replace("José","J",$in_list);
It doesn't get the new value, José is still there. Am i missing something? thanx in advance.
How exactly do you try to replace the string?
When trying it this way:
$in_list = str_replace("José","J",$in_list);
echo $in_list;
everything should work fine.
Remember, the function is returning a value. So it returns a new string.
This should work. It depends on your array.
$str = array('Robert','Emmanuel','José','Alexander');
$str = implode(",", $str);
print str_replace('José', 'J', $str);
I'm not sure what's going on, It seems to work for me. What version of PHP are you using?
$in_list = "'".implode("','", array('Robert', 'Emmanuel', 'José', 'Alexander'))."'";
$replaced = str_replace("José", "J", $in_list);
//prints 'Robert','Emmanuel','J','Alexander'
echo $replaced;
See: http://codepad.viper-7.com/24qutm
try $in_list = html_entity_decode((str_replace(htmlentities("José"),"J",htmlentities($in_list));
Have you tried on a word without accents? I would say you have a character set mismatch, for example 'José' in $in_list is in latin1 character set and your PHP source file in UTF8.
If this is the case, you should first convert either your PHP file or the variable to the character set you want to work with.
Spontaneous guess: Those two strings are not the same. I suppose one "José" is a string hardcoded in your source code and the other is received from the database or the browser or so. If the encoding of the two strings is not the same, PHP won't identify them as identical and not replace the character. Make sure your source code file is saved in the same encoding as the data you're working on, preferably both being UTF-8.
This worked for me, but it doesn't look like I'm doing anything notably different from you?
$array = array('Robert', 'Emmanuel', 'José', 'Alexander');
$in_list = "'".implode("','",$array)."'";
echo $in_list.PHP_EOL;
echo str_replace("José","J",$in_list).PHP_EOL;
Output:
'Robert','Emmanuel','José','Alexander'
'Robert','Emmanuel','J','Alexander'
Keep in mind that str_replace will not perform the replacement on $in_list itself, but rather return a string containing the replacement.
Hope this helps!

PHP doesn't detect white space in string

I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);

Searching through a string trying to find ' in PHP

I am using tinyMCE and, rather annoyingly, it replaces all of my apostrophes with their HTML numeric equivalent. Now most of the time this isn't a problem but for some reason I am having a problem storing the apostrophe replacement. So i have to search through the string and replace them all. Any help would be much appreciated
did you try:
$string = str_replace("'", "<replacement>", $string);
Is it just apostrophes that you want decoded from HTML entities, or everything?
print html_entity_decode("Hello, that's an apostophe.", ENT_QUOTE);
will print
Hello, that's an apostrophe.
Why work around the problem when you can fix the cause? You can just turn of the TinyMCE entity encoding*. More info: here
*Unless you want all the other characters encoded, that is.

Categories