Mb_encode_mimeheader in PHP - php

I'm trying to use mb_encode_mimeheader but I'm getting an error that it's an undefined function. Yet I believe it is part of native PHP.
Here is my code:
$encoded_subject = mb_encode_mimeheader('my foreign character string', 'UTF-8', 'B');
Is this function part of PHP that I need to extend or something? I've used this function before and never had any problems.

In order to use the mb_* functions, you need to ensure that the mbstring extension is available/compiled in. (It's not a default extension in terms of the "standard" install, but it is commonly available as a part of most well configured ISP offerings.)
See the Multibyte String installing/configuring section within the PHP manual for more information.

http://php.net/mbstring.installation
As mentioned there its not enabled by default. there is also mentioned, how to enable it.

Related

Removing var_dump from PHP code

We have a large codebase, and every so often a var_dump used for testing and not removed/commented suddenly appears out of nowhere. There is a messy solution using XDebug (http://devzone.zend.com/1135/tracing-php-applications-with-xdebug/), but maybe there's something ingenous that can be done in PHP at runtime.
Also, I don't want to modify or search code via regex. I've tried using my own var_dump_v2, but it falls out of use quickly.
Is it possible to use the disable_functions operation in php.ini to disable var_dump on your production server? I am not sure what the outcome of this setting is (ie does it fail with an error, or silently) the documentation is not so clear.
http://php.net/manual/en/ini.core.php - see "disable_functions"
Also there is override_function:
<?php
override_function('var_dump', '$a', 'return 0;');
?>
http://php.net/manual/en/function.override-function.php
There are actually ways to do this, if you have PECL available and runkit installed. You kan make runkit able to overide PHPs internal functions if you in php.ini set runkit.internal_override to "1".
For removing the var_dump function, you could use:
runkit_function_remove('var_dump');
In your case, not to get an error, you should probably instead use something like this:
runkit_function_redefine('var_dump', '','');
Take a look at the runkit extensions documentation here.
You may also want to take a look at "Advanced PHP debugger", another extension that seems to offer an override_function().
You can use monkey patching.
Just defines a namespace on the first line of your file and defines the function var_dump
<?php
namespace monkey;
function var_dump($obj) {}
Of course, it implies that you do not use a namespace in your current file
You could use the function var_dump() prefixing it with the root namespace(): \var_dump()
Of course, all others native function will continue to work as usual as long as you do not override them in your namespace.
Why don't you use serialize() or json_encode() if you have a large database? That will be very useful.
But take note, serialize() will give you a 1-line output somewhat like this:
's:0:"";s:5:"value";'
So you need to learn the anatomy of serialize() to use it: PHP Serialize

Replace native PHP functions to make it unicode compliant

Currently a (PHP5.4) web application I manage does not support unicode characters, so I want to convert it in order to support unicode (utf-8). Since future version (PHP6) may offer native unicode support I would like to keep the original native functions (e.g. trim(), strtoupper() etc) intact. Basically I want to override existing built in functions to do that.
The solution so far I came up with was to create one big include file to each page with the relevant functions and use namespaces, so for example:
namespace my_ns;
mb_internal_encoding("UTF-8");
function strtoupper()
{
$args = func_get_args();
return call_user_func_array('mb_strtoupper', $args );
}
However, in order for this to work i would have to add the namespace to each file, since namespaces are apparently bound to a single php-file.
My question is: is this the best way to go forward or are there better methods? Is there a way to 'autoload' namespaces automatically?
Help appreciated
update:
I found changing mbstring.func_overload in the php.ini file can override the existing functions, so that answers my question I guess.

Determine extension that PHP function belongs to?

Sometime I find a function in the PHP manual that I want to use, but when I try to use it - it's undefined.
How can I consistently find which extension a function belongs to? It doesn't seem to be written in the PHP manual.
For example, mb_strlen belongs to the mbstring extension.
Given A, how do I consistently find B?
It's in the manual. See the sidebar on the left:
Multibyte String
Multibyte String Functions
Click either one of those and then go to the introduction to read about the extension. Then check out the Installing/Configuring page.

Remove function alias in PHP

Is there a way to remove function alias in PHP?
I can rename my function but it would be nice to use name "fetch".
Problem:
I just tested the following code and it appears to work for me, but perhaps it is because I don't have the mysqli library installed. I would test it because it might be more contextual than your IDE will have you believe. It seems to be a method for mysqli, but it might not be a global function.
<?php
function fetch(){
echo 'Hello world!';
}
fetch();
No.
(Short of recompiling the PHP binary)
This is more of a function of the IDE than the actual language... Some IDEs may give you that ability... I don't even know if recompiling the PHP binary (as Alan Storm suggested) would help since sometimes the stuff is hardcoded into the IDE / use the PHP docs online
For completeness sake: Normally, no, this can not be done. However: this can be done using a PECL extension called "runkit".
Runkit is described as "For all those things you probably shouldn't have been doing anyway", and allows you to basically tear out the innards of PHP from within PHP itself. Replacing built-in functions, undefining constants, unloading classes - suddenly everything is possible. And you should really question what you are doing if you ever feel you need it - odds are what you are doing violates some principles that are there for very good reasons, you just don't know them yet. I've never found a situation where using Runkit was a genuinely Good Idea.
Oh, in order to remove built-in functions you'll specifically need to enable this capability in your php.ini
(have fun!)

My php can't find 'normalizer_normalize()' function, why?

How come I can't use normalizer function?
I am using it exactly as I should, ie: $var = normalizer_normalize($str);
I get php error: no such function!
Here is the code:
$headline= mysql_real_escape_string($_POST['headline']);
$ad_id_string=normalizer_normalize($headline);
Thanks
From the docs, normalizer_normalize was included in PHP as of version 5.3.0. Prior to that, it was available as a PECL extension. To build it as part of PHP, you'll need to install internationalization support.
If you need to provide your own equivalent to the Normalizer class, strtr() would be a good option. The only downside is that you will need to provide your own character strings to translate to / from.

Categories