Help with replacing characters - php
Hopefully someone can help out here;
I am trying to write a function which replaces special characters and returns the correct one.
This is what I have so far:
function convertlatin($output){
$latinchar = array("€", "‚","Æ'","„","…","‡","ˆ","‰","Å","‹","Å'",'Ž','‘','’','“','â€','•','â€"','â€"','Ëœ','â"¢','Å¡','›','Å"',"ž",'Ÿ','¡','¢','£','¤','Â¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼',"½",'¾','¿','À','Ã','Â','Ã','Ã"','Ã…','Æ','Ç','È','É','Ê','Ë','ÃŒ ','Ã','ÃŽ','ß','Ã',"Ã'","Ã'",'Ã"','Ã"','Õ','Ö','×','Ø','Ù','Ú','Û','Ãœ','Ã','Þ','ß','Ã','á','â','ã','ä','Ã¥','æ','ç','è','é','ê','ë','ì','Ã','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý',"þ","ÿ");
$correctchar = array("€", "‚","ƒ",'"','…','‡','ˆ','‰',"Š",'‹','Œ','Ž',"'","'",'"','"','•','–','—','˜','™','š','›','œ','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ',"ÿ");
$returnval = str_replace($latinchar, $correctchar, $output);
echo($returnval);
return $returnval;
}
The problem I have is I thought it was working but it has random results, such as if it finds a match on just one of the characters it replaces a different one in that array. What I would like to do is find and replace an exact match of latin char within a supplied string eg "testingÿ" with "testingÿ" - at the mo it replaces ÿ with testingá¿
It just seems to replace one character in some occasions, when I would like it to match and replace both parameters.
I also tried strcmp with not much success.
Any ideas ?
Looks like your problem is not wrong chars, it's more a wrong encoding. Maybe you better try to change the encoding of $output. utf8_encode will not help you, the "wrong" chars look like some wrong converted Windows-1252-input.
Try:
echo mb_convert_encoding('testingÿ','CP1252','UTF-8');
Related
Php function substr blanks the whole string if there's a capital letter with an accent mark
TLDR: strlen('Ángel Gallardo Perez',0,15) returns blank. But 'Angél Gállardo Pérez' has no problems. How to use strlen with a capital marked accent letter? Longer: Hello. Currently I'm having problem showing a list of clients. Due to the language being spanish I've got to show some letters with a marked accent. Non-capital letters are not a problem (áéíóú) but capital accentuated letters are (ÁÉÍÓÚ) Currently am showing the name like this: strlen($tempnombre) > 15 ? substr($tempnombre,0,15)."..." : $tempnombre; The idea being that if you've got a short name, I show it as it is. But if its long, I shorten it and add the "..." When I've got a name like Ángel, strlen returns nothing. I'm sure its that function and that function alone since everything else just returns the name OK. If I just return the name as is, then it shows perfectly. I've tried a couple of things already: $tempnombre = $this->nombre; mb_convert_case(utf8_encode($tempnombre), MB_CASE_TITLE, 'UTF-8'); //NAME HERE IS STILL WORKING OK, dd($tempnombre) -> returns OK return strlen($tempnombre) > 15 ? substr($tempnombre,0,15)."..." : $tempnombre; Any thoughts? Thanks in advance.
Just found it after posting the question... If anyone needs it the proper way is as follows: strlen($this->nombre) > 15 ? mb_substr($this->nombre,0,15,'UTF-8')."..." : $this->nombre; Use mb_substr instead of substr.
PHP Replace all characters with a symbol
I am trying to make an account generator with censured passwords, and I don't want to replace all characters with just 10 *'s. I want it to be like this: if the password is 15 characters long, it will be replaced with 15 *'s. I tried to use this: $censpass = preg_replace('/[a-zA-Z0-9\']/', '*', $accounts[$i]['password']); but as you might know, that doesn't work for !'s. How can I use preg_replace with every single character in PHP? If someone doesn't understand: I want this: "password123!" to be replaced with this: "************" with the accurate length using preg_replace If this exists somewhere else, please link it below, I tried to find this but I could only find how to replace some characters, like numbers only Thank you :)
For your goal I'd use a different approach, such as: $encpass = str_pad('', strlen($accounts[$i]['password']), '*'); In fact, there is no need to use a regular expression (which is slow and resource consuming) just to generate a string the same length as another one. Anyway, if you still want to use your solution, the correct regexp for your use case is simply a . such as: $censpass = preg_replace('/./', '*', $accounts[$i]['password']); Have a look here: http://php.net/manual/en/regexp.reference.dot.php
Using preg_match_all to filter out strings containing this but not this
im having an issue with preg_match_all. I have this string: $product_req = "ACTIVE-6,CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-9"; I need to get the numbers preceded by "ACTIVE-" but not by "CATEGORY-ACTIVE-", so in this case the result should be 6,9. I used the statement below: preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act); However this will return all the numbers because all of them are in fact preceded by "ACTIVE-" but thats not what i meant because i need to leave out those preceded by "CATEGORY-ACTIVE-". How can i configure preg_match_all to do it? Or maybe there is some other function that can do the job? EDIT: I tried this: preg_match_all("/CATEGORY-ACTIVE-(\d+)/", $product_req, $this_cat_act); preg_match_all("/ACTIVE-(\d+)/", $product_req, $this_act); $act_cat = str_replace($this_cat_act[1],"",$this_act[1]); it kinda works, but i guess there is a better and cleaner way to do it. Besides the output is kinda weird too. Thank you.
PHP: preg_match; Not able to match the £ symbol
I've really been wracking my brains over this one, as for the life of me I can't figure out what the problem is. I've got some data I want to run a regular expression on. For reference, the original document is encoded in iso-8859-15, if that makes any difference. Here is a function using the regular expression; if(preg_match("{£\d+\.\d+}", $handle)) // { echo 'Found a match'; } else { echo 'No match found'; } No matter what I try I can't seem to get it to match. I've tried just searching for the £ symbol. I've gone over my regular expression and there aren't any issues there. I've even pasted the source data directly into a regular expression tester and it finds a complete match for what I'm looking for. I just don't understand why my regular expression isn't working. I've looked at the raw data in my string that I'm searching for and the £ symbol is there as clear as day. I get the feeling that there's some encoded character there that I just can't see, but no matter how I output the data all I can see is the £ symbol, but for whatever reason it's not being recognised. Any ideas? Is there an absolute method to viewing raw data in a string? I've tried var_dump and var_export, but I do get the feeling that something isn't quite right, as var_export does display the data in a different language. How can I see what's "really" there in my variable? I've even saved the content to a txt file. The £ is there. There should be no reason why I shouldn't be able to find it with my regular expression. I just don't get it. If I create a string and paste in the exact bit of test my regular expression should pick up, it finds the match without any problems. Truly baffling.
You could always transform the letter: $string = '£100.00'; if(preg_match("/\xa3/",$string)){ echo 'match found'; }else{ echo 'no matches'; }
You can include any character in your regular expression if you know the hexadecimal value. I think the value is 0A3H, so try this: \xa3 // Updated with the correct hex value
PHP - quick regular expression question
so I am trying to match word in a wall of text and return few words before and after the match. Everything is working, but I would like to ask if there is any way to modify it so it will look for similar words. Hmm, let me show you an example: preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*(pripravená)(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]); This code returns a match, but I would like it to modify it so preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*(pripravena)(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]); would also return a match. Its slovak language and I tried with range of unicode characters and also with \p{Sk} (and few others) but to no avail. Maybe I just put it in the wrong place, I dont know... Is something like this possible? Any help is appreciated
I don't know if there is a "ignore accent" switch. But you could replace your search query with something like: $query = 'pripravená'; $query = preg_replace( array('=[áàâa]=i','=[óòôo]=i','=[úùûu]=i'), array( '[áàâa]' , '[óòôo]' , '[úùûu]' ), $query ); preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*('.$query.')(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]); That would convert your 'pripravená' query into 'pripraven[áàâa]'.
You could use strtr() to strip out the accents: See the PHP manual page for a good example - http://php.net/manual/en/function.strtr.php $addr = strtr($addr, "äåö", "aao"); You'd still need to specify all the relevant characters, but it would be easier than using a regex to do it.
(pripraven[áa]) or (pripravena\p{M}*) or, more likely, some combination of these approaches. I don't know of any other, more concise, way of specifying "all Latin-1 vowels that are similar to 'a' in my current locale".