![]() |
Character replacing inside a string - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Character replacing inside a string (/showthread.php?tid=25244) |
Character replacing inside a string - El Forum - 12-05-2009 [eluser]developer10[/eluser] Hello, it'll be the best if i simply illustrate my problem with an example: i want to write a function which would replace non-English character by english ones: $str = "Ovo je nešto o čemu sada nećemo raspravljati"; into $str = "ovo-je-nesto-o-cemu-sada-necemo-raspravljati"; I'm a bit familiar with the str_replace() function, but how to make it to search for characters (č,ć,đ,š,ž) in one step and replace them by c,c,dj,s,z respectively? for now, i can do this: Code: function f_ime_seo($replace,$bythis,$str) and it works well, but the problem is i can put into $replace only one character and i want to be able to check for existence of all those character in one step. Any suggestions would be appreciated! Thanks Character replacing inside a string - El Forum - 12-05-2009 [eluser]Jamie Rumbelow[/eluser] Using str_replace you can't do it in one step - I'd suggest writing one function that looks for every letter and replaces them one by one. Someone else will probably suggest a much better performing solution but this will do: Code: function replace_all($string) { Character replacing inside a string - El Forum - 12-05-2009 [eluser]developer10[/eluser] this is what i found on this issue and it works well when adapted to my case (at least for now): Code: $string = 'The quick brown fox jumped over the lazy dog.'; thanks anyway, maybe i'll come back! ![]() Character replacing inside a string - El Forum - 12-05-2009 [eluser]rossmurphy[/eluser] I'm curious as to why you need to remove those characters? Character replacing inside a string - El Forum - 12-05-2009 [eluser]developer10[/eluser] [quote author="rossmurphy" date="1260054600"]I'm curious as to why you need to remove those characters?[/quote] because i need to use those strings in my URLs and i cannot find the way to decode/convert them to work properly. i can permit them in URIs but still they cause me problems when i need to use them as variables in my queries. Character replacing inside a string - El Forum - 12-05-2009 [eluser]hugle[/eluser] Hello you can also do smth liek this: Code: $find = array ('ą', 'č', 'ę'); You could also try mb_str_replace, it handles UTF8 better, much better! good luck! |