This article and everyone to share is mainly PHP7 Extension Development in the processing of strings related knowledge, hope that through the sharing of this article can help you better learning php .
this time, let's take a look at the string what to do with PHP extensions.
The sample code is as follows:
<?phpfunction str_concat($prefix, $string) {
$len = strlen ($prefix);
$substr = substr ($string, 0, $len);
if ($substr! = $prefix) {
return $prefix. " ". $string;
} else {
return $string;
}
}
Echo str_concat ("Hello", "word"); Echo "\ n"; Echo str_concat ("Hello", "Hello bo56.com"); Echo "\ n";? >
the Str_concat method above implements the following functions:
1. When the string does not contain the specified prefix string, the prefix string and the detected word are met and returned.
2. When the string contains the specified prefix string, it is returned as-is.
we will use PHP extension to implement Str_concat functionality.
code
implementing The Str_concat method
Str_concat method of PHP extension Source:
php_function (str_concat)
{
zend_string *prefix, *subject, *result;
Zval *string;
If (Zend_parse_parameters (Zend_num_args (), "Sz", &prefix, &string) = = FAILURE) {
return;
}
Subject = zval_get_string (string);
If (zend_binary_strncmp (zstr_val (prefix), Zstr_len (prefix), zstr_val (subject) , Zstr_len (subject), Zstr_len (prefix)) = = 0) {
return_str (subject);
}
result = strpprintf (0, "%s%s", Zstr_val (prefix), zstr_val (subject));
return_str (result);
}
Code Description
zend_string is The new structure of PHP7. The structure is as follows:
struct _zend_string {
Zend_refcounted_h GC; /*GC Information * *
Zend_ulong H; /* Hash Value */
size_t Len; /* string length * /
char val[1]; /* string start address * /
};
Some methods of zend_string processing are provided in zend/zend_string.h .
The macro method at the beginning of Zstr_ is a proprietary method of the zend_string structure. There are several main:
#define ZSTR_VAL (zstr) (zstr)->val
#define Zstr_len (zstr) (zstr)->len
#define ZSTR_H (zstr) (zstr)->h
#define Zstr_hash (zstr) zend_string_hash_val (zstr)
zstr_val zstr_len macro method corresponds to zend_stringzstr_hash is the Hash hash The function generates one.
The second parameter is deliberately converted to Zval in the code . The main purpose is to show that Zend provides us with a number of ways to manipulate the columns. such as,zval_get_string, zend_binary_strncmp.
These methods are Zend/zend_operators.h file.
Source: Knowledgeable and worry-free
PHP7 string processing of extended development