php extension in-out parameter - php

I'm writing a php extension using c++. The c++ function signature is:
bool validateAndNormalizeStr (string& str)
Here 'str' is a in-out parameter. It's value would be updated in the function.
I hope the php function has the same signature. Here is a use case:
$str = " zh-hans-cn";
$ret = validateAndNormalizeStr($str);
if($ret)
{
echo $str;
}
I hope the output is: "zh-Hans-CN".
The problem is I haven't found any document about in-out parameter handling in writing php extension. Help!!!
Thanks

Related

"Warning: preg_replace_callback(): Requires argument 2, '&#x;', to be a valid callback" ; PHP Error

Setting up an App that should put Ads over images and I'm getting this WordPress error when running the code and it's been a frustrating week trying to fix this thing.
function cwali_json_encode_utf8callback($matches){
return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
}
function cwali_json_encode_utf8($json)
{
return preg_replace_callback('/\\\\u(\w{4})/', cwali_json_encode_utf8callback($matches), json_encode($json));
}
I expect it to work but it gives me the following error : Warning: preg_replace_callback(): Requires argument 2, '&#x;', to be a valid callback.
If someone could please come up with a solution, that would be amazing, thank you so much!
In your call
preg_replace_callback('/\\\\u(\w{4})/', cwali_json_encode_utf8callback($matches), json_encode($json));
instead of passing the callback function, you call the function, so its return value is passed as the argument. Try
preg_replace_callback('/\\\\u(\w{4})/', 'cwali_json_encode_utf8callback', json_encode($json));

Having issues converting VBA.NET function to PHP

There's a following function written in VBA.NET which I'm trying convert to PHP. I'm primarly a PHP developer, and I have alsmost zero-knowledge in VB.
<%
Public Shared Function ComputeHash(ByVal Key As String) As String
Dim objSHA1 As New SHA1CryptoServiceProvider
objSHA1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Key.ToCharArray))
Dim buffer() As Byte = objSHA1.Hash
Dim HashValue As String = System.Convert.ToBase64String(buffer)
Return HashValue
End Function
%>
I have tried searching for manuals and found some guidelances.
This is so far what I have converted to PHP myself:
function compute_hash($string){
return base64_encode(sha1($string));
}
Hovewer it doesn't produce the same result as VB's function ComputeHash(). Coudn't someone tell what I'm doing wrong?
Example of execution on both languages:
VBA.NET
string = "orange45877687459999SENTRYORD01154321"
ComputeHash(string) // Returns = WbwSWEBzPqgo9C4nZmGwHhd/FBQ=
PHP
$string = "orange45877687459999SENTRYORD01154321";
compute_hash(string) // Returns = NTliYzEyNTg0MDczM2VhODI4ZjQyZTI3NjY2MWIwMWUxNzdmMTQxNA== (but I need "WbwSWEBzPqgo9C4nZmGwHhd/FBQ=")
You need to instruct PHP to return the SHA1-hash in bytes instead of in hex format, by passing the TRUE parameter to the hashing function:
function compute_hash($string){
return base64_encode(sha1($string, TRUE));
}
PHP manual for SHA1

What does the is_final parameter in xml_parse() do?

I'm working on creating a class for xml parsing, and need to use this method from PHP called xml_parse.
It has 3 parameters like so:
int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )
According to the PHP manual is_final it means whether its the last piece of data sent in this parse, but what does this mean? does this something have to do with resource $parser? as far as I'm aware this function does not allow an input stream of data, thus my confusion.
Someone please explain what it does
is_final means that if you are parsing the last line of your $data you must set this parameter to true.
in addition there is a note in the Doc :
Entity errors are reported at the end of the parse. And will only show if the "end" parameter is TRUE
See the sample below from w3schools
<?php
$parser=xml_parser_create();
function char($parser,$data)
{
echo $data;
}
xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096))
{
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
?>

PHP equivalent of .NET Code

I am converting .net project to PHP . But unable to convert the following code:
public static string HashString(string value)
{
SHA1 hasher = new SHA1CryptoServiceProvider();
UTF8Encoding enc = new UTF8Encoding();
byte[] hashInBytes = hasher.ComputeHash(enc.GetBytes(value));
return Convert.ToBase64String(hashInBytes);
}
So far I have done this but result is not same:
function HashString($str) {
return base64_encode(sha1($str));
}
Please help, thanks.
The reason behind the difference is, PHP uses ASCII encoding for hash calculations.
In C#, you can replace UTF8Encoding with ASCIIEncoding in order to have same results.
Finally I found the solution:
This is the final code which is equivalent to .net code:
function HashString($str) {
return base64_encode(sha1($str,true));
}
I have added "true" with sha1 function.

php - need help converting a python command to php code

I'm using the python hashlib to get a condensed md5 value using the following command, can someone help me out with a compatible function?
hashlib.md5('my string').hexdigest()
$string = 'my string'
$encoded_string = md5($string);
echo md5('apple');
// => 1f3870be274f6c49b3e31a0c6728957f
produces the same result as
require 'digest/md5';
print Digest::MD5.hexdigest('apple');
# => 1f3870be274f6c49b3e31a0c6728957f

Categories