How do I modify a string printed by a script?
I'm having problems with wordpress mishandling accented characters, or maybe t开发者_开发知识库he problem is with some plugin. Whichever the case I need to "translate" some strings, removing accents which i do with:
$value = strtr($value, $trans);
I really need to change this string which renders the user's location but it's printed with a script so I have no idea how to do it:
<script language="javascript"> document.write('' + geoip_city() +''); </script>
Is there a way to assign it's result to a php value beforehand or something? Maybe removing the accents with a script. I really need to modify it, how could I manage to do it? Thanks
The only way I could see this happening is by using output buffering, see
http://php.net/manual/en/function.ob-start.php
and
http://www.php.net/manual/en/function.ob-get-clean.php
Though if at all possible I would avoid using this, it's not exactly best practice. See if the function in question takes any arguments to return the data rather than print it and otherwise duplicate the function and make the changes you need to it.
What is the codepage of the page with the script and what is the codepage of the string returned? If your page has a
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
and the data is utf8 then change to
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
or vice-versa
If you need to do it on the client you can do
a)
<script language="javascript">
var x = geoip_city();
</script>
and use a script like this one
b) if you cannot access the script but CAN add a script yourself:
var oldWrite = document.write;
document.write = function(str) {
var newString = "";
// your code to convert here
oldWrite(newString);
}
I would suggest trying to take the string output of the "geoip_city()" and do your search and replace there.
Using your example code:
<script language="javascript"> document.write('' + geoip_city().toString().replace('searchStr', 'replaceStr') + ''); </script>
More information about string methods can be found on Microsoft's JScript or Mozilla's MDC pages: http://msdn.microsoft.com/en-us/library/t0kbytzc(v=vs.85).aspx
Can't link to more than one page, but if you do a Google search for replace MDC, you'll find it.
精彩评论