1999-04-17 08:37:12 +08:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
1999-07-16 21:13:16 +08:00
| PHP version 4.0 |
1999-04-17 08:37:12 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
1999-07-16 21:13:16 +08:00
| Copyright ( c ) 1997 , 1998 , 1999 The PHP Group |
1999-04-17 08:37:12 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
1999-07-16 21:13:16 +08:00
| This source file is subject to version 2.0 of the PHP license , |
| that is bundled with this package in the file LICENSE , and is |
| available at through the world - wide - web at |
| http : //www.php.net/license/2_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
1999-04-17 08:37:12 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Authors : Rasmus Lerdorf < rasmus @ lerdorf . on . ca > |
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
| Stig S <EFBFBD> ther Bakken < ssb @ fast . no > |
1999-07-16 21:13:16 +08:00
| Zeev Suraski < zeev @ zend . com > |
1999-04-17 08:37:12 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
1999-04-24 04:06:01 +08:00
1999-12-18 12:01:20 +08:00
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-04-17 08:37:12 +08:00
# include <stdio.h>
# include "php.h"
1999-06-16 05:51:00 +08:00
# include "reg.h"
# include "post.h"
1999-12-05 03:19:57 +08:00
# include "php_string.h"
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
# ifdef HAVE_LOCALE_H
# include <locale.h>
1999-04-17 08:37:12 +08:00
# endif
1999-06-16 05:51:00 +08:00
# include "zend_execute.h"
# include "php_globals.h"
1999-04-17 08:37:12 +08:00
1999-12-05 22:16:37 +08:00
int php_tag_find ( char * tag , int len , char * set ) ;
1999-11-28 08:31:02 +08:00
/* this is read-only, so it's ok */
1999-05-16 19:12:23 +08:00
static char hexconvtab [ ] = " 0123456789abcdef " ;
1999-05-16 19:32:51 +08:00
static char * php_bin2hex ( const unsigned char * old , const size_t oldlen , size_t * newlen )
1999-05-16 19:12:23 +08:00
{
1999-05-16 19:32:51 +08:00
unsigned char * new = NULL ;
1999-08-12 18:53:29 +08:00
size_t i , j ;
1999-05-16 19:12:23 +08:00
new = ( char * ) emalloc ( oldlen * 2 * sizeof ( char ) ) ;
if ( ! new ) {
return new ;
}
for ( i = j = 0 ; i < oldlen ; i + + ) {
new [ j + + ] = hexconvtab [ old [ i ] > > 4 ] ;
new [ j + + ] = hexconvtab [ old [ i ] & 15 ] ;
}
if ( newlen ) * newlen = oldlen * 2 * sizeof ( char ) ;
return new ;
}
/* proto bin2hex(string data)
converts the binary representation of data to hex */
PHP_FUNCTION ( bin2hex )
{
1999-09-25 19:55:42 +08:00
pval * * data ;
1999-05-16 19:12:23 +08:00
char * new ;
size_t newlen ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & data ) = = FAILURE ) {
1999-05-16 19:12:23 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( data ) ;
1999-05-16 19:12:23 +08:00
1999-09-25 19:55:42 +08:00
new = php_bin2hex ( ( * data ) - > value . str . val , ( * data ) - > value . str . len , & newlen ) ;
1999-05-16 19:12:23 +08:00
if ( ! new ) {
RETURN_FALSE ;
}
RETURN_STRINGL ( new , newlen , 0 ) ;
}
1999-04-17 08:37:12 +08:00
/* {{{ proto int strspn(string str, string mask)
Find length of initial segment consisting entirely of characters found in mask */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strspn )
1999-04-17 08:37:12 +08:00
{
1999-12-14 07:40:36 +08:00
zval * * s1 , * * s2 ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
1999-12-14 07:40:36 +08:00
RETURN_LONG ( php_strspn ( ( * s1 ) - > value . str . val , ( * s2 ) - > value . str . val ,
( * s1 ) - > value . str . val + ( * s1 ) - > value . str . len ,
( * s2 ) - > value . str . val + ( * s2 ) - > value . str . len ) ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto int strcspn(string str, string mask)
Find length of initial segment consisting entirely of characters not found in mask */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strcspn )
1999-04-17 08:37:12 +08:00
{
1999-12-14 11:25:04 +08:00
zval * * s1 , * * s2 ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
1999-12-14 07:40:36 +08:00
RETURN_LONG ( php_strcspn ( ( * s1 ) - > value . str . val , ( * s2 ) - > value . str . val ,
( * s1 ) - > value . str . val + ( * s1 ) - > value . str . len ,
( * s2 ) - > value . str . val + ( * s2 ) - > value . str . len ) ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
1999-10-17 00:35:20 +08:00
PHPAPI void php_trim ( pval * str , pval * return_value , int mode )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* mode 1 : trim left
mode 2 : trim right
mode 3 : trim left and right
*/
{
register int i ;
int len = str - > value . str . len ;
int trimmed = 0 ;
char * c = str - > value . str . val ;
if ( mode & 1 ) {
for ( i = 0 ; i < len ; i + + ) {
if ( c [ i ] = = ' ' | | c [ i ] = = ' \n ' | | c [ i ] = = ' \r ' | |
c [ i ] = = ' \t ' | | c [ i ] = = ' \v ' ) {
trimmed + + ;
} else {
break ;
}
}
len - = trimmed ;
c + = trimmed ;
}
if ( mode & 2 ) {
for ( i = len - 1 ; i > = 0 ; i - - ) {
if ( c [ i ] = = ' ' | | c [ i ] = = ' \n ' | | c [ i ] = = ' \r ' | |
c [ i ] = = ' \t ' | | c [ i ] = = ' \v ' ) {
len - - ;
} else {
break ;
}
}
}
RETVAL_STRINGL ( c , len , 1 ) ;
}
/* {{{ proto string rtrim(string str)
An alias for chop */
/* }}} */
1999-04-17 08:37:12 +08:00
/* {{{ proto string chop(string str)
Remove trailing whitespace */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( chop )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * str ) - > type = = IS_STRING ) {
1999-10-17 00:35:20 +08:00
php_trim ( * str , return_value , 2 ) ;
1999-04-17 08:37:12 +08:00
return ;
}
RETURN_FALSE ;
}
/* }}} */
/* {{{ proto string trim(string str)
Strip whitespace from the beginning and end of a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( trim )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * str ) - > type = = IS_STRING ) {
1999-10-17 00:35:20 +08:00
php_trim ( * str , return_value , 3 ) ;
1999-04-17 08:37:12 +08:00
return ;
}
RETURN_FALSE ;
}
/* }}} */
/* {{{ proto string ltrim(string str)
Strip whitespace from the beginning of a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( ltrim )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
if ( ( * str ) - > type = = IS_STRING ) {
1999-10-17 00:35:20 +08:00
php_trim ( * str , return_value , 1 ) ;
1999-04-17 08:37:12 +08:00
return ;
}
RETURN_FALSE ;
}
/* }}} */
1999-10-17 00:35:20 +08:00
PHPAPI void php_explode ( pval * delim , pval * str , pval * return_value )
1999-04-17 08:37:12 +08:00
{
1999-12-07 02:29:22 +08:00
char * p1 , * p2 , * endp ;
1999-04-17 08:37:12 +08:00
int i = 0 ;
1999-12-07 02:29:22 +08:00
endp = str - > value . str . val + str - > value . str . len ;
p1 = str - > value . str . val ;
p2 = php_memnstr ( str - > value . str . val , delim - > value . str . val , delim - > value . str . len , endp ) ;
1999-04-17 08:37:12 +08:00
if ( p2 = = NULL ) {
1999-12-07 02:29:22 +08:00
add_index_stringl ( return_value , i + + , p1 , str - > value . str . len , 1 ) ;
1999-12-07 16:14:00 +08:00
} else {
do {
add_index_stringl ( return_value , i + + , p1 , p2 - p1 , 1 ) ;
p1 = p2 + delim - > value . str . len ;
} while ( ( p2 = php_memnstr ( p1 , delim - > value . str . val , delim - > value . str . len , endp ) ) ! = NULL ) ;
if ( p1 < = endp ) {
add_index_stringl ( return_value , i + + , p1 , endp - p1 , 1 ) ;
}
1999-04-17 08:37:12 +08:00
}
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* {{{ proto array explode(string separator, string str)
Split a string on string separator and return array of components */
PHP_FUNCTION ( explode )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str , * * delim ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & delim , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-12-07 02:29:22 +08:00
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
convert_to_string_ex ( delim ) ;
1999-04-17 08:37:12 +08:00
1999-12-07 02:29:22 +08:00
if ( ! ( * delim ) - > value . str . len ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " Empty delimiter " ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
RETURN_FALSE ;
}
1999-12-07 02:29:22 +08:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( array_init ( return_value ) = = FAILURE ) {
1999-12-07 02:29:22 +08:00
RETURN_FALSE ;
1999-04-17 08:37:12 +08:00
}
1999-12-07 02:29:22 +08:00
1999-10-17 00:35:20 +08:00
php_explode ( * delim , * str , return_value ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
/* }}} */
1999-10-15 22:45:54 +08:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* {{{ proto string join(array src, string glue)
An alias for implode */
/* }}} */
1999-10-17 00:35:20 +08:00
PHPAPI void php_implode ( pval * delim , pval * arr , pval * return_value )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
{
1999-06-21 23:57:20 +08:00
pval * * tmp ;
1999-12-12 04:00:40 +08:00
int len = 0 , count = 0 , target = 0 ;
1999-04-17 08:37:12 +08:00
/* convert everything to strings, and calculate length */
1999-08-03 03:17:14 +08:00
zend_hash_internal_pointer_reset ( arr - > value . ht ) ;
while ( zend_hash_get_current_data ( arr - > value . ht , ( void * * ) & tmp ) = = SUCCESS ) {
1999-10-15 22:45:54 +08:00
convert_to_string_ex ( tmp ) ;
1999-12-12 04:00:40 +08:00
len + = ( * tmp ) - > value . str . len ;
if ( count > 0 ) {
len + = delim - > value . str . len ;
1999-04-17 08:37:12 +08:00
}
1999-12-12 04:00:40 +08:00
count + + ;
1999-08-03 03:17:14 +08:00
zend_hash_move_forward ( arr - > value . ht ) ;
1999-04-17 08:37:12 +08:00
}
/* do it */
return_value - > value . str . val = ( char * ) emalloc ( len + 1 ) ;
return_value - > value . str . val [ 0 ] = ' \0 ' ;
return_value - > value . str . val [ len ] = ' \0 ' ;
1999-08-03 03:17:14 +08:00
zend_hash_internal_pointer_reset ( arr - > value . ht ) ;
while ( zend_hash_get_current_data ( arr - > value . ht , ( void * * ) & tmp ) = = SUCCESS ) {
1999-12-12 04:00:40 +08:00
count - - ;
memcpy ( return_value - > value . str . val + target , ( * tmp ) - > value . str . val ,
( * tmp ) - > value . str . len ) ;
target + = ( * tmp ) - > value . str . len ;
if ( count > 0 ) {
memcpy ( return_value - > value . str . val + target , delim - > value . str . val ,
delim - > value . str . len ) ;
target + = delim - > value . str . len ;
1999-04-17 08:37:12 +08:00
}
1999-08-03 03:17:14 +08:00
zend_hash_move_forward ( arr - > value . ht ) ;
1999-04-17 08:37:12 +08:00
}
return_value - > type = IS_STRING ;
return_value - > value . str . len = len ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* {{{ proto string implode(array src, string glue)
Join array elements placing glue string between items and return one string */
PHP_FUNCTION ( implode )
{
1999-09-25 19:55:42 +08:00
pval * * arg1 , * * arg2 , * delim , * arr ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & arg1 , & arg2 ) = = FAILURE ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
if ( ( * arg1 ) - > type = = IS_ARRAY & & ( * arg2 ) - > type = = IS_STRING ) {
1999-10-15 22:45:54 +08:00
SEPARATE_ZVAL ( arg1 ) ;
1999-09-25 19:55:42 +08:00
arr = * arg1 ;
delim = * arg2 ;
} else if ( ( * arg2 ) - > type = = IS_ARRAY ) {
1999-10-15 22:53:56 +08:00
SEPARATE_ZVAL ( arg2 )
1999-09-25 19:55:42 +08:00
arr = * arg2 ;
1999-10-15 22:53:56 +08:00
convert_to_string_ex ( arg1 ) ;
1999-09-25 19:55:42 +08:00
delim = * arg1 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
} else {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " Bad arguments to %s() " ,
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
get_active_function_name ( ) ) ;
return ;
}
1999-10-17 00:35:20 +08:00
php_implode ( delim , arr , return_value ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
1999-04-17 08:37:12 +08:00
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-04-17 08:37:12 +08:00
/* {{{ proto string strtok([string str,] string token)
Tokenize a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strtok )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str , * * tok ;
1999-04-17 08:37:12 +08:00
char * token = NULL , * tokp = NULL ;
char * first = NULL ;
int argc ;
1999-11-28 08:31:02 +08:00
BLS_FETCH ( ) ;
1999-04-17 08:37:12 +08:00
argc = ARG_COUNT ( ht ) ;
1999-12-19 06:40:35 +08:00
if ( ( argc = = 1 & & zend_get_parameters_ex ( 1 , & tok ) = = FAILURE ) | |
( argc = = 2 & & zend_get_parameters_ex ( 2 , & str , & tok ) = = FAILURE ) | |
1999-04-17 08:37:12 +08:00
argc < 1 | | argc > 2 ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( tok ) ;
tokp = token = ( * tok ) - > value . str . val ;
1999-04-17 08:37:12 +08:00
if ( argc = = 2 ) {
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-11-28 08:31:02 +08:00
STR_FREE ( BG ( strtok_string ) ) ;
BG ( strtok_string ) = estrndup ( ( * str ) - > value . str . val , ( * str ) - > value . str . len ) ;
BG ( strtok_pos1 ) = BG ( strtok_string ) ;
BG ( strtok_pos2 ) = NULL ;
1999-04-17 08:37:12 +08:00
}
1999-11-28 08:31:02 +08:00
if ( BG ( strtok_pos1 ) & & * BG ( strtok_pos1 ) ) {
1999-04-17 08:37:12 +08:00
for ( /* NOP */ ; token & & * token ; token + + ) {
1999-11-28 08:31:02 +08:00
BG ( strtok_pos2 ) = strchr ( BG ( strtok_pos1 ) , ( int ) * token ) ;
if ( ! first | | ( BG ( strtok_pos2 ) & & BG ( strtok_pos2 ) < first ) ) {
first = BG ( strtok_pos2 ) ;
1999-04-17 08:37:12 +08:00
}
} /* NB: token is unusable now */
1999-11-28 08:31:02 +08:00
BG ( strtok_pos2 ) = first ;
if ( BG ( strtok_pos2 ) ) {
* BG ( strtok_pos2 ) = ' \0 ' ;
1999-04-17 08:37:12 +08:00
}
1999-11-28 08:31:02 +08:00
RETVAL_STRING ( BG ( strtok_pos1 ) , 1 ) ;
1999-04-17 08:37:12 +08:00
#if 0
/* skip 'token' white space for next call to strtok */
1999-11-28 08:31:02 +08:00
while ( BG ( strtok_pos2 ) & &
strchr ( tokp , * ( BG ( strtok_pos2 ) + 1 ) ) ) {
BG ( strtok_pos2 ) + + ;
1999-04-17 08:37:12 +08:00
}
# endif
1999-11-28 08:31:02 +08:00
if ( BG ( strtok_pos2 ) )
BG ( strtok_pos1 ) = BG ( strtok_pos2 ) + 1 ;
1999-04-17 08:37:12 +08:00
else
1999-11-28 08:31:02 +08:00
BG ( strtok_pos1 ) = NULL ;
1999-04-17 08:37:12 +08:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
1999-12-10 07:45:30 +08:00
PHPAPI char * php_strtoupper ( char * s , size_t len )
1999-04-17 08:37:12 +08:00
{
char * c ;
int ch ;
1999-12-10 07:45:30 +08:00
int i ;
1999-04-17 08:37:12 +08:00
c = s ;
1999-12-10 07:45:30 +08:00
for ( i = 0 ; i < len ; i + + ) {
1999-04-17 08:37:12 +08:00
ch = toupper ( ( unsigned char ) * c ) ;
* c + + = ch ;
}
return ( s ) ;
}
/* {{{ proto string strtoupper(string str)
Make a string uppercase */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strtoupper )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * arg ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( arg ) ;
1999-04-17 08:37:12 +08:00
1999-09-28 02:00:30 +08:00
* return_value = * * arg ;
zval_copy_ctor ( return_value ) ;
1999-12-10 07:45:30 +08:00
php_strtoupper ( return_value - > value . str . val , return_value - > value . str . len ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
1999-12-10 07:45:30 +08:00
PHPAPI char * php_strtolower ( char * s , size_t len )
1999-04-17 08:37:12 +08:00
{
register int ch ;
char * c ;
1999-12-10 12:44:32 +08:00
int i ;
1999-04-17 08:37:12 +08:00
c = s ;
1999-12-10 07:45:30 +08:00
for ( i = 0 ; i < len ; i + + ) {
1999-04-17 08:37:12 +08:00
ch = tolower ( ( unsigned char ) * c ) ;
* c + + = ch ;
}
return ( s ) ;
}
/* {{{ proto string strtolower(string str)
Make a string lowercase */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strtolower )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
char * ret ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-28 02:00:30 +08:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
1999-12-10 07:45:30 +08:00
ret = php_strtolower ( return_value - > value . str . val , return_value - > value . str . len ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto string basename(string path)
Return the filename component of the path */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( basename )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
char * ret , * c ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
ret = estrdup ( ( * str ) - > value . str . val ) ;
c = ret + ( * str ) - > value . str . len - 1 ;
1999-04-17 08:37:12 +08:00
while ( * c = = ' / '
# ifdef MSVC5
| | * c = = ' \\ '
# endif
)
c - - ;
* ( c + 1 ) = ' \0 ' ;
if ( ( c = strrchr ( ret , ' / ' ) )
# ifdef MSVC5
| | ( c = strrchr ( ret , ' \\ ' ) )
# endif
) {
RETVAL_STRING ( c + 1 , 1 ) ;
} else {
1999-09-25 19:55:42 +08:00
RETVAL_STRING ( ( * str ) - > value . str . val , 1 ) ;
1999-04-17 08:37:12 +08:00
}
efree ( ret ) ;
}
/* }}} */
1999-10-17 00:35:20 +08:00
PHPAPI void php_dirname ( char * str , int len ) {
1999-04-17 08:37:12 +08:00
register char * c ;
c = str + len - 1 ;
while ( * c = = ' / '
# ifdef MSVC5
| | * c = = ' \\ '
# endif
)
c - - ; /* strip trailing slashes */
* ( c + 1 ) = ' \0 ' ;
if ( ( c = strrchr ( str , ' / ' ) )
# ifdef MSVC5
| | ( c = strrchr ( str , ' \\ ' ) )
# endif
)
* c = ' \0 ' ;
}
/* {{{ proto string dirname(string path)
Return the directory name component of the path */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( dirname )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
char * ret ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
ret = estrdup ( ( * str ) - > value . str . val ) ;
1999-10-17 00:35:20 +08:00
php_dirname ( ret , ( * str ) - > value . str . len ) ;
1999-04-17 08:37:12 +08:00
RETVAL_STRING ( ret , 1 ) ;
efree ( ret ) ;
}
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* case insensitve strstr */
1999-12-12 03:50:01 +08:00
PHPAPI char * php_stristr ( unsigned char * s , unsigned char * t ,
size_t s_len , size_t t_len )
1999-04-17 08:37:12 +08:00
{
1999-12-12 03:50:01 +08:00
php_strtolower ( s , s_len ) ;
php_strtolower ( t , t_len ) ;
return php_memnstr ( s , t , t_len , s + s_len ) ;
1999-04-17 08:37:12 +08:00
}
1999-12-14 07:40:36 +08:00
PHPAPI size_t php_strspn ( char * s1 , char * s2 , char * s1_end , char * s2_end )
{
register const char * p = s1 , * spanp ;
register char c = * p ;
cont :
for ( spanp = s2 ; p ! = s1_end & & spanp ! = s2_end ; )
if ( * spanp + + = = c ) {
c = * ( + + p ) ;
goto cont ;
}
return ( p - s1 ) ;
}
PHPAPI size_t php_strcspn ( char * s1 , char * s2 , char * s1_end , char * s2_end )
{
register const char * p , * spanp ;
register char c = * s1 ;
for ( p = s1 ; ; ) {
spanp = s2 ;
do {
if ( * spanp = = c | | p = = s1_end )
return ( p - s1 ) ;
} while ( spanp + + < s2_end ) ;
c = * ( + + p ) ;
}
/* NOTREACHED */
}
1999-08-10 04:52:58 +08:00
/* {{{ proto string stristr(string haystack, string needle)
1999-04-17 08:37:12 +08:00
Find first occurrence of a string within another , case insensitive */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( stristr )
1999-04-17 08:37:12 +08:00
{
1999-12-12 03:50:01 +08:00
zval * * haystack , * * needle ;
1999-04-17 08:37:12 +08:00
char * found = NULL ;
1999-12-12 03:50:01 +08:00
char needle_char [ 2 ] ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 08:37:12 +08:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
1999-04-17 08:37:12 +08:00
1999-12-12 03:50:01 +08:00
if ( ( * needle ) - > type = = IS_STRING ) {
if ( ( * needle ) - > value . str . len = = 0 ) {
php_error ( E_WARNING , " Empty delimiter " ) ;
RETURN_FALSE ;
}
found = php_stristr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . str . val ,
( * haystack ) - > value . str . len , ( * needle ) - > value . str . len ) ;
} else {
convert_to_long_ex ( needle ) ;
needle_char [ 0 ] = tolower ( ( char ) ( * needle ) - > value . lval ) ;
needle_char [ 1 ] = ' \0 ' ;
found = php_stristr ( ( * haystack ) - > value . str . val , needle_char ,
( * haystack ) - > value . str . len , 1 ) ;
1999-04-17 08:37:12 +08:00
}
if ( found ) {
1999-12-12 03:50:01 +08:00
RETVAL_STRING ( found , 1 ) ;
1999-04-17 08:37:12 +08:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
/* {{{ proto string strstr(string haystack, string needle)
Find first occurrence of a string within another */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strstr )
1999-04-17 08:37:12 +08:00
{
1999-12-12 03:50:01 +08:00
zval * * haystack , * * needle ;
char * haystack_end ;
1999-04-17 08:37:12 +08:00
char * found = NULL ;
1999-12-12 03:50:01 +08:00
char needle_char [ 2 ] ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 08:37:12 +08:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
1999-12-12 03:50:01 +08:00
haystack_end = ( * haystack ) - > value . str . val + ( * haystack ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > type = = IS_STRING ) {
1999-12-12 03:50:01 +08:00
if ( ( * needle ) - > value . str . len = = 0 ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " Empty delimiter " ) ;
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-12-12 03:50:01 +08:00
found = php_memnstr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . str . val ,
( * needle ) - > value . str . len , haystack_end ) ;
1999-04-17 08:37:12 +08:00
} else {
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( needle ) ;
1999-12-12 03:50:01 +08:00
needle_char [ 0 ] = ( char ) ( * needle ) - > value . lval ;
needle_char [ 1 ] = ' \0 ' ;
found = php_memnstr ( ( * haystack ) - > value . str . val , needle_char , 1 , haystack_end ) ;
1999-04-17 08:37:12 +08:00
}
if ( found ) {
1999-12-12 03:50:01 +08:00
RETVAL_STRING ( found , 1 ) ;
1999-04-17 08:37:12 +08:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
1999-08-10 04:52:58 +08:00
/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* {{{ proto int strpos(string haystack, string needle [, int offset])
1999-04-17 08:37:12 +08:00
Find position of first occurrence of a string within another */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strpos )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * haystack , * * needle , * * OFFSET ;
1999-04-17 08:37:12 +08:00
int offset = 0 ;
char * found = NULL ;
1999-12-07 01:37:59 +08:00
char * endp ;
char * startp ;
1999-04-17 08:37:12 +08:00
switch ( ARG_COUNT ( ht ) ) {
case 2 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 2 , & haystack , & needle ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
break ;
case 3 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 3 , & haystack , & needle , & OFFSET ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( OFFSET ) ;
offset = ( * OFFSET ) - > value . lval ;
1999-12-07 01:37:59 +08:00
if ( offset < 0 ) {
php_error ( E_WARNING , " offset not contained in string " ) ;
RETURN_FALSE ;
}
1999-04-17 08:37:12 +08:00
break ;
default :
WRONG_PARAM_COUNT ;
}
1999-12-07 01:37:59 +08:00
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
1999-12-07 01:37:59 +08:00
1999-09-25 19:55:42 +08:00
if ( offset > ( * haystack ) - > value . str . len ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " offset not contained in string " ) ;
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-12-07 01:37:59 +08:00
startp = ( * haystack ) - > value . str . val ;
startp + = offset ;
endp = ( * haystack ) - > value . str . val ;
endp + = ( * haystack ) - > value . str . len ;
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > type = = IS_STRING ) {
if ( ( * needle ) - > value . str . len = = 0 ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " Empty delimiter " ) ;
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-12-07 01:37:59 +08:00
found = php_memnstr ( startp , ( * needle ) - > value . str . val , ( * needle ) - > value . str . len , endp ) ;
1999-04-17 08:37:12 +08:00
} else {
1999-12-07 01:37:59 +08:00
char buf ;
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( needle ) ;
1999-12-07 01:37:59 +08:00
buf = ( char ) ( * needle ) - > value . lval ;
found = php_memnstr ( startp , & buf , 1 , endp ) ;
1999-04-17 08:37:12 +08:00
}
if ( found ) {
1999-09-25 19:55:42 +08:00
RETVAL_LONG ( found - ( * haystack ) - > value . str . val ) ;
1999-04-17 08:37:12 +08:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
/* {{{ proto int strrpos(string haystack, string needle)
Find the last occurrence of a character in a string within another */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strrpos )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * haystack , * * needle ;
1999-04-17 08:37:12 +08:00
char * found = NULL ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > type = = IS_STRING ) {
found = strrchr ( ( * haystack ) - > value . str . val , * ( * needle ) - > value . str . val ) ;
1999-04-17 08:37:12 +08:00
} else {
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( needle ) ;
found = strrchr ( ( * haystack ) - > value . str . val , ( char ) ( * needle ) - > value . lval ) ;
1999-04-17 08:37:12 +08:00
}
if ( found ) {
1999-09-25 19:55:42 +08:00
RETVAL_LONG ( ( * haystack ) - > value . str . len - strlen ( found ) ) ;
1999-04-17 08:37:12 +08:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
/* {{{ proto string strrchr(string haystack, string needle)
Find the last occurrence of a character in a string within another */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strrchr )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * haystack , * * needle ;
1999-04-17 08:37:12 +08:00
char * found = NULL ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 08:37:12 +08:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > type = = IS_STRING ) {
found = strrchr ( ( * haystack ) - > value . str . val , * ( * needle ) - > value . str . val ) ;
1999-04-17 08:37:12 +08:00
} else {
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( needle ) ;
found = strrchr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . lval ) ;
1999-04-17 08:37:12 +08:00
}
if ( found ) {
RETVAL_STRING ( found , 1 ) ;
} else {
RETVAL_FALSE ;
}
}
/* }}} */
1999-10-17 00:35:20 +08:00
static char * php_chunk_split ( char * src , int srclen , char * end , int endlen ,
int chunklen , int * destlen )
1999-04-17 08:37:12 +08:00
{
char * dest ;
char * p , * q ;
int chunks ; /* complete chunks! */
int restlen ;
chunks = srclen / chunklen ;
restlen = srclen - chunks * chunklen ; /* srclen % chunklen */
dest = emalloc ( ( srclen + ( chunks + 1 ) * endlen + 1 ) * sizeof ( char ) ) ;
for ( p = src , q = dest ; p < ( src + srclen - chunklen + 1 ) ; ) {
memcpy ( q , p , chunklen ) ;
q + = chunklen ;
memcpy ( q , end , endlen ) ;
q + = endlen ;
p + = chunklen ;
}
if ( restlen ) {
memcpy ( q , p , restlen ) ;
q + = restlen ;
memcpy ( q , end , endlen ) ;
q + = endlen ;
}
* q = ' \0 ' ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( destlen ) {
* destlen = q - dest ;
}
1999-04-17 08:37:12 +08:00
return ( dest ) ;
}
/* {{{ proto string chunk_split(string str [, int chunklen [, string ending]])
Return split line */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( chunk_split )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * p_str , * * p_chunklen , * * p_ending ;
1999-04-17 08:37:12 +08:00
int argc ;
char * result ;
char * end = " \r \n " ;
int endlen = 2 ;
int chunklen = 76 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
int result_len ;
1999-04-17 08:37:12 +08:00
argc = ARG_COUNT ( ht ) ;
1999-06-16 05:51:00 +08:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( argc < 1 | | argc > 3 | |
1999-12-19 06:40:35 +08:00
zend_get_parameters_ex ( argc , & p_str , & p_chunklen , & p_ending ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-04-17 08:37:12 +08:00
switch ( argc ) {
case 3 :
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( p_ending ) ;
end = ( * p_ending ) - > value . str . val ;
endlen = ( * p_ending ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
case 2 :
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( p_chunklen ) ;
chunklen = ( * p_chunklen ) - > value . lval ;
1999-04-17 08:37:12 +08:00
case 1 :
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( p_str ) ;
1999-04-17 08:37:12 +08:00
}
if ( chunklen = = 0 ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " chunk length is 0 " ) ;
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-10-17 00:35:20 +08:00
result = php_chunk_split ( ( * p_str ) - > value . str . val , ( * p_str ) - > value . str . len ,
end , endlen , chunklen , & result_len ) ;
1999-04-17 08:37:12 +08:00
if ( result ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
RETVAL_STRINGL ( result , result_len , 0 ) ;
1999-04-17 08:37:12 +08:00
} else {
RETURN_FALSE ;
}
}
/* }}} */
/* {{{ proto string substr(string str, int start [, int length])
Return part of a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( substr )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * string , * * from , * * len ;
1999-04-17 08:37:12 +08:00
int argc , l ;
int f ;
argc = ARG_COUNT ( ht ) ;
1999-12-19 06:40:35 +08:00
if ( ( argc = = 2 & & zend_get_parameters_ex ( 2 , & string , & from ) = = FAILURE ) | |
( argc = = 3 & & zend_get_parameters_ex ( 3 , & string , & from , & len ) = = FAILURE ) | |
1999-04-17 08:37:12 +08:00
argc < 2 | | argc > 3 ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( string ) ;
convert_to_long_ex ( from ) ;
f = ( * from ) - > value . lval ;
1999-04-17 08:37:12 +08:00
if ( argc = = 2 ) {
1999-09-25 19:55:42 +08:00
l = ( * string ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
} else {
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( len ) ;
l = ( * len ) - > value . lval ;
1999-04-17 08:37:12 +08:00
}
/* if "from" position is negative, count start position from the end
* of the string
*/
if ( f < 0 ) {
1999-09-25 19:55:42 +08:00
f = ( * string ) - > value . str . len + f ;
1999-04-17 08:37:12 +08:00
if ( f < 0 ) {
f = 0 ;
}
}
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if ( l < 0 ) {
1999-09-25 19:55:42 +08:00
l = ( ( * string ) - > value . str . len - f ) + l ;
1999-04-17 08:37:12 +08:00
if ( l < 0 ) {
l = 0 ;
}
}
1999-09-25 19:55:42 +08:00
if ( f > = ( int ) ( * string ) - > value . str . len ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
if ( ( f + l ) > ( int ) ( * string ) - > value . str . len ) {
l = ( int ) ( * string ) - > value . str . len - f ;
1999-04-17 08:37:12 +08:00
}
1999-06-16 05:51:00 +08:00
1999-09-25 19:55:42 +08:00
RETVAL_STRINGL ( ( * string ) - > value . str . val + f , l , 1 ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
1999-11-17 06:44:13 +08:00
1999-11-21 06:09:27 +08:00
/* {{{ proto string substr_replace(string str, string repl, int start [, int length])
1999-11-17 06:44:13 +08:00
Replace part of a string with another string */
PHP_FUNCTION ( substr_replace )
{
zval * * string ;
zval * * from ;
zval * * len ;
zval * * repl ;
char * result ;
int result_len ;
int argc ;
int l ;
int f ;
argc = ARG_COUNT ( ht ) ;
1999-12-19 06:40:35 +08:00
if ( ( argc = = 3 & & zend_get_parameters_ex ( 3 , & string , & repl , & from ) = = FAILURE ) | |
( argc = = 4 & & zend_get_parameters_ex ( 4 , & string , & repl , & from , & len ) = = FAILURE ) | |
1999-11-21 06:09:27 +08:00
argc < 3 | | argc > 4 ) {
1999-11-17 06:44:13 +08:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( string ) ;
1999-11-21 06:09:27 +08:00
convert_to_string_ex ( repl ) ;
1999-11-17 06:44:13 +08:00
convert_to_long_ex ( from ) ;
f = ( * from ) - > value . lval ;
1999-11-21 06:09:27 +08:00
if ( argc = = 3 ) {
1999-11-17 06:44:13 +08:00
l = ( * string ) - > value . str . len ;
} else {
convert_to_long_ex ( len ) ;
l = ( * len ) - > value . lval ;
}
/* if "from" position is negative, count start position from the end
* of the string
*/
if ( f < 0 ) {
f = ( * string ) - > value . str . len + f ;
if ( f < 0 ) {
f = 0 ;
}
}
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if ( l < 0 ) {
l = ( ( * string ) - > value . str . len - f ) + l ;
if ( l < 0 ) {
l = 0 ;
}
}
if ( f > = ( int ) ( * string ) - > value . str . len ) {
RETURN_STRINGL ( ( * string ) - > value . str . val , ( * string ) - > value . str . len , 1 ) ;
}
if ( ( f + l ) > ( int ) ( * string ) - > value . str . len ) {
l = ( int ) ( * string ) - > value . str . len - f ;
}
1999-11-21 06:09:27 +08:00
result_len = ( * string ) - > value . str . len - l + ( * repl ) - > value . str . len ;
1999-11-17 06:44:13 +08:00
result = ( char * ) ecalloc ( result_len + 1 , sizeof ( char * ) ) ;
1999-12-10 07:45:30 +08:00
memcpy ( result , ( * string ) - > value . str . val , f ) ;
memcpy ( & result [ f ] , ( * repl ) - > value . str . val , ( * repl ) - > value . str . len ) ;
memcpy ( & result [ f + ( * repl ) - > value . str . len ] , ( * string ) - > value . str . val + f + l ,
( * string ) - > value . str . len - f - l ) ;
1999-11-17 06:44:13 +08:00
1999-12-10 07:45:30 +08:00
RETVAL_STRINGL ( result , result_len , 0 ) ;
1999-11-17 06:44:13 +08:00
}
/* }}} */
1999-04-17 08:37:12 +08:00
/* {{{ proto string quotemeta(string str)
Quote meta characters */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( quotemeta )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * arg ;
1999-04-17 08:37:12 +08:00
char * str , * old ;
char * p , * q ;
char c ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( arg ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
old = ( * arg ) - > value . str . val ;
1999-04-17 08:37:12 +08:00
if ( ! * old ) {
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
str = emalloc ( 2 * ( * arg ) - > value . str . len + 1 ) ;
1999-04-17 08:37:12 +08:00
for ( p = old , q = str ; ( c = * p ) ; p + + ) {
switch ( c ) {
case ' . ' :
case ' \\ ' :
case ' + ' :
case ' * ' :
case ' ? ' :
case ' [ ' :
case ' ^ ' :
case ' ] ' :
case ' $ ' :
case ' ( ' :
case ' ) ' :
* q + + = ' \\ ' ;
/* break is missing _intentionally_ */
default :
* q + + = c ;
}
}
* q = 0 ;
RETVAL_STRING ( erealloc ( str , q - str + 1 ) , 0 ) ;
}
/* }}} */
/* {{{ proto int ord(string character)
Return ASCII value of character */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( ord )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
RETVAL_LONG ( ( unsigned char ) ( * str ) - > value . str . val [ 0 ] ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto string chr(int ascii)
Convert ASCII code to a character */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( chr )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * num ;
1999-04-17 08:37:12 +08:00
char temp [ 2 ] ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & num ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( num ) ;
temp [ 0 ] = ( char ) ( * num ) - > value . lval ;
1999-04-17 08:37:12 +08:00
temp [ 1 ] = ' \0 ' ;
RETVAL_STRINGL ( temp , 1 , 1 ) ;
}
/* }}} */
1999-08-10 04:52:58 +08:00
/* {{{ proto string ucfirst(string str)
1999-04-17 08:37:12 +08:00
Make a string ' s first character uppercase */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( ucfirst )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * arg ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( arg ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ! * ( * arg ) - > value . str . val ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-11-15 01:20:56 +08:00
* return_value = * * arg ;
zval_copy_ctor ( return_value ) ;
* return_value - > value . str . val = toupper ( ( unsigned char ) * return_value - > value . str . val ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto string ucwords(string str)
Uppercase the first character of every word in a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( ucwords )
1999-04-17 08:37:12 +08:00
{
1999-12-12 03:50:01 +08:00
zval * * str ;
1999-04-17 08:37:12 +08:00
char * r ;
1999-12-12 03:50:01 +08:00
char * r_end ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-12-12 03:50:01 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-12-12 03:50:01 +08:00
if ( ! ( * str ) - > value . str . len ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-12-12 03:50:01 +08:00
* return_value = * * str ;
1999-11-15 01:20:56 +08:00
zval_copy_ctor ( return_value ) ;
* return_value - > value . str . val = toupper ( ( unsigned char ) * return_value - > value . str . val ) ;
r = return_value - > value . str . val ;
1999-12-12 03:50:01 +08:00
r_end = r + ( * str ) - > value . str . len ;
while ( ( r = php_memnstr ( r , " " , 1 , r_end ) ) ! = NULL ) {
if ( r < r_end ) {
1999-04-17 08:37:12 +08:00
r + + ;
* r = toupper ( ( unsigned char ) * r ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
} else {
break ;
}
1999-04-17 08:37:12 +08:00
}
}
/* }}} */
1999-10-17 00:35:20 +08:00
PHPAPI char * php_strtr ( char * string , int len , char * str_from ,
char * str_to , int trlen )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
{
int i ;
unsigned char xlat [ 256 ] ;
if ( ( trlen < 1 ) | | ( len < 1 ) ) {
return string ;
}
for ( i = 0 ; i < 256 ; xlat [ i ] = i , i + + ) ;
for ( i = 0 ; i < trlen ; i + + ) {
xlat [ ( unsigned char ) str_from [ i ] ] = str_to [ i ] ;
}
for ( i = 0 ; i < len ; i + + ) {
string [ i ] = xlat [ ( unsigned char ) string [ i ] ] ;
}
return string ;
}
1999-11-20 23:53:18 +08:00
static void php_strtr_array ( zval * return_value , char * str , int slen , HashTable * hash )
{
zval * entry ;
char * string_key ;
zval * * trans ;
zval ctmp ;
ulong num_key ;
int minlen = 128 * 1024 ;
int maxlen = 0 , pos , len , newpos , newlen , found ;
char * newstr , * key ;
zend_hash_internal_pointer_reset ( hash ) ;
while ( zend_hash_get_current_data ( hash , ( void * * ) & entry ) = = SUCCESS ) {
switch ( zend_hash_get_current_key ( hash , & string_key , & num_key ) ) {
case HASH_KEY_IS_STRING :
len = strlen ( string_key ) ;
if ( len > maxlen ) maxlen = len ;
if ( len < minlen ) minlen = len ;
efree ( string_key ) ;
break ;
case HASH_KEY_IS_LONG :
ctmp . type = IS_LONG ;
ctmp . value . lval = num_key ;
convert_to_string ( & ctmp ) ;
len = ctmp . value . str . len ;
zval_dtor ( & ctmp ) ;
if ( len > maxlen ) maxlen = len ;
if ( len < minlen ) minlen = len ;
break ;
}
zend_hash_move_forward ( hash ) ;
}
key = emalloc ( maxlen + 1 ) ;
newstr = emalloc ( 8192 ) ;
newlen = 8192 ;
newpos = pos = 0 ;
while ( pos < slen ) {
if ( ( pos + maxlen ) > slen ) {
maxlen = slen - pos ;
}
found = 0 ;
1999-11-21 20:05:39 +08:00
memcpy ( key , str + pos , maxlen ) ;
1999-11-20 23:53:18 +08:00
for ( len = maxlen ; len > = minlen ; len - - ) {
key [ len ] = 0 ;
if ( zend_hash_find ( hash , key , len + 1 , ( void * * ) & trans ) = = SUCCESS ) {
char * tval ;
int tlen ;
zval tmp ;
if ( ( * trans ) - > type ! = IS_STRING ) {
tmp = * * trans ;
zval_copy_ctor ( & tmp ) ;
convert_to_string ( & tmp ) ;
tval = tmp . value . str . val ;
tlen = tmp . value . str . len ;
} else {
tval = ( * trans ) - > value . str . val ;
tlen = ( * trans ) - > value . str . len ;
}
if ( ( newpos + tlen + 1 ) > newlen ) {
newlen = newpos + tlen + 1 + 8192 ;
newstr = realloc ( newstr , newlen ) ;
}
memcpy ( newstr + newpos , tval , tlen ) ;
newpos + = tlen ;
pos + = len ;
found = 1 ;
if ( ( * trans ) - > type ! = IS_STRING ) {
zval_dtor ( & tmp ) ;
}
break ;
}
}
if ( ! found ) {
if ( ( newpos + 1 ) > newlen ) {
newlen = newpos + 1 + 8192 ;
newstr = realloc ( newstr , newlen ) ;
}
newstr [ newpos + + ] = str [ pos + + ] ;
}
}
efree ( key ) ;
newstr [ newpos ] = 0 ;
RETURN_STRINGL ( newstr , newpos , 0 ) ;
}
1999-04-17 08:37:12 +08:00
/* {{{ proto string strtr(string str, string from, string to)
Translate characters in str using given translation tables */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strtr )
1999-04-17 08:37:12 +08:00
{ /* strtr(STRING,FROM,TO) */
1999-09-25 19:55:42 +08:00
pval * * str , * * from , * * to ;
1999-11-20 23:53:18 +08:00
int ac = ARG_COUNT ( ht ) ;
1999-12-19 06:40:35 +08:00
if ( ac < 2 | | ac > 3 | | zend_get_parameters_ex ( ac , & str , & from , & to ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-11-20 23:53:18 +08:00
if ( ac = = 2 & & ( * from ) - > type ! = IS_ARRAY ) {
php_error ( E_WARNING , " arg2 must be passed an array " ) ;
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-11-20 23:53:18 +08:00
if ( ac = = 2 ) {
php_strtr_array ( return_value , ( * str ) - > value . str . val , ( * str ) - > value . str . len , HASH_OF ( * from ) ) ;
} else {
convert_to_string_ex ( from ) ;
convert_to_string_ex ( to ) ;
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
php_strtr ( return_value - > value . str . val ,
return_value - > value . str . len ,
( * from ) - > value . str . val ,
( * to ) - > value . str . val ,
MIN ( ( * from ) - > value . str . len , ( * to ) - > value . str . len ) ) ;
}
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto string strrev(string str)
Reverse a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( strrev )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
int i , len ;
char c ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
len = ( * str ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
for ( i = 0 ; i < len - 1 - i ; i + + ) {
1999-09-25 19:55:42 +08:00
c = ( * str ) - > value . str . val [ i ] ;
( * str ) - > value . str . val [ i ] = ( * str ) - > value . str . val [ len - 1 - i ] ;
( * str ) - > value . str . val [ len - 1 - i ] = c ;
1999-04-17 08:37:12 +08:00
}
1999-09-25 19:55:42 +08:00
* return_value = * * str ;
1999-04-17 08:37:12 +08:00
pval_copy_constructor ( return_value ) ;
}
/* }}} */
1999-10-17 00:35:20 +08:00
static void php_similar_str ( const char * txt1 , int len1 , const char * txt2 ,
int len2 , int * pos1 , int * pos2 , int * max )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
{
char * p , * q ;
char * end1 = ( char * ) txt1 + len1 ;
char * end2 = ( char * ) txt2 + len2 ;
int l ;
* max = 0 ;
for ( p = ( char * ) txt1 ; p < end1 ; p + + ) {
for ( q = ( char * ) txt2 ; q < end2 ; q + + ) {
for ( l = 0 ; ( p + l < end1 ) & & ( q + l < end2 ) & & ( p [ l ] = = q [ l ] ) ;
l + + ) ;
if ( l > * max ) {
* max = l ;
* pos1 = p - txt1 ;
* pos2 = q - txt2 ;
}
}
}
}
1999-10-17 00:35:20 +08:00
static int php_similar_char ( const char * txt1 , int len1 ,
const char * txt2 , int len2 )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
{
int sum ;
int pos1 , pos2 , max ;
1999-10-17 00:35:20 +08:00
php_similar_str ( txt1 , len1 , txt2 , len2 , & pos1 , & pos2 , & max ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( ( sum = max ) ) {
if ( pos1 & & pos2 )
1999-10-17 00:35:20 +08:00
sum + = php_similar_char ( txt1 , pos1 , txt2 , pos2 ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( ( pos1 + max < len1 ) & & ( pos2 + max < len2 ) )
1999-10-17 00:35:20 +08:00
sum + = php_similar_char ( txt1 + pos1 + max , len1 - pos1 - max ,
txt2 + pos2 + max , len2 - pos2 - max ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
return sum ;
}
/* {{{ proto int similar_text(string str1, string str2 [, double percent])
Calculates the similarity between two strings */
PHP_FUNCTION ( similar_text )
{
1999-09-25 19:55:42 +08:00
pval * * t1 , * * t2 , * * percent ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
int ac = ARG_COUNT ( ht ) ;
int sim ;
if ( ac < 2 | | ac > 3 | |
1999-12-19 06:40:35 +08:00
zend_get_parameters_ex ( ac , & t1 , & t2 , & percent ) = = FAILURE ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( t1 ) ;
convert_to_string_ex ( t2 ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( ac > 2 ) {
1999-09-25 19:55:42 +08:00
convert_to_double_ex ( percent ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
1999-09-25 19:55:42 +08:00
if ( ( ( * t1 ) - > value . str . len + ( * t2 ) - > value . str . len ) = = 0 ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( ac > 2 ) {
1999-09-25 19:55:42 +08:00
( * percent ) - > value . dval = 0 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
RETURN_LONG ( 0 ) ;
}
1999-10-17 00:35:20 +08:00
sim = php_similar_char ( ( * t1 ) - > value . str . val , ( * t1 ) - > value . str . len ,
1999-09-25 19:55:42 +08:00
( * t2 ) - > value . str . val , ( * t2 ) - > value . str . len ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
if ( ac > 2 ) {
1999-09-25 19:55:42 +08:00
( * percent ) - > value . dval = sim * 200.0 / ( ( * t1 ) - > value . str . len + ( * t2 ) - > value . str . len ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
RETURN_LONG ( sim ) ;
}
/* }}} */
1999-04-17 08:37:12 +08:00
/* be careful, this edits the string in-place */
1999-09-04 21:18:59 +08:00
PHPAPI void php_stripslashes ( char * string , int * len )
1999-04-17 08:37:12 +08:00
{
char * s , * t ;
int l ;
char escape_char = ' \\ ' ;
1999-06-16 05:51:00 +08:00
PLS_FETCH ( ) ;
1999-04-17 08:37:12 +08:00
if ( PG ( magic_quotes_sybase ) ) {
escape_char = ' \' ' ;
}
if ( len ! = NULL ) {
l = * len ;
} else {
l = strlen ( string ) ;
}
s = string ;
t = string ;
while ( l > 0 ) {
if ( * t = = escape_char ) {
t + + ; /* skip the slash */
if ( len ! = NULL )
( * len ) - - ;
l - - ;
if ( l > 0 ) {
if ( * t = = ' 0 ' ) {
* s + + = ' \0 ' ;
t + + ;
} else {
* s + + = * t + + ; /* preserve the next character */
}
l - - ;
}
} else {
if ( s ! = t )
* s + + = * t + + ;
else {
s + + ;
t + + ;
}
l - - ;
}
}
if ( s ! = t ) {
* s = ' \0 ' ;
}
}
1999-09-06 04:55:13 +08:00
/* {{{ proto string addcslashes(string str, string charlist)
Escape all chars mentioned in charlist with backslash . It creates
octal representations if asked to backslash characters with 8 th bit set
or with ASCII < 32 ( except ' \n ' , ' \r ' , ' \t ' etc . . . ) */
PHP_FUNCTION ( addcslashes )
{
1999-09-25 19:55:42 +08:00
pval * * str , * * what ;
1999-09-06 04:55:13 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & str , & what ) = = FAILURE ) {
1999-09-06 04:55:13 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
convert_to_string_ex ( what ) ;
return_value - > value . str . val = php_addcslashes ( ( * str ) - > value . str . val , ( * str ) - > value . str . len , & return_value - > value . str . len , 0 , ( * what ) - > value . str . val , ( * what ) - > value . str . len ) ;
1999-09-06 04:55:13 +08:00
return_value - > type = IS_STRING ;
}
/* }}} */
1999-04-17 08:37:12 +08:00
/* {{{ proto string addslashes(string str)
Escape single quote , double quotes and backslash characters in a string with backslashes */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( addslashes )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
return_value - > value . str . val = php_addslashes ( ( * str ) - > value . str . val , ( * str ) - > value . str . len , & return_value - > value . str . len , 0 ) ;
1999-04-17 08:37:12 +08:00
return_value - > type = IS_STRING ;
}
/* }}} */
1999-09-06 04:55:13 +08:00
/* {{{ proto string stripcslashes(string str)
Strip backslashes from a string . Uses C - style conventions */
PHP_FUNCTION ( stripcslashes )
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-09-06 04:55:13 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-09-06 04:55:13 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-09-06 04:55:13 +08:00
1999-09-28 02:00:30 +08:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
1999-09-06 04:55:13 +08:00
php_stripcslashes ( return_value - > value . str . val , & return_value - > value . str . len ) ;
}
/* }}} */
1999-04-17 08:37:12 +08:00
/* {{{ proto string stripslashes(string str)
Strip backslashes from a string */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( stripslashes )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-28 02:00:30 +08:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
1999-09-04 21:18:59 +08:00
php_stripslashes ( return_value - > value . str . val , & return_value - > value . str . len ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
# ifndef HAVE_STRERROR
# if !APACHE
char * strerror ( int errnum )
{
extern int sys_nerr ;
extern char * sys_errlist [ ] ;
1999-11-28 08:31:02 +08:00
BLS_FETCH ( ) ;
1999-04-17 08:37:12 +08:00
if ( ( unsigned int ) errnum < sys_nerr ) return ( sys_errlist [ errnum ] ) ;
1999-11-28 08:31:02 +08:00
( void ) sprintf ( BG ( str_ebuf ) , " Unknown error: %d " , errnum ) ;
return ( BG ( str_ebuf ) ) ;
1999-04-17 08:37:12 +08:00
}
# endif
# endif
1999-09-06 04:55:13 +08:00
PHPAPI void php_stripcslashes ( char * str , int * len )
{
char * source , * target , * end ;
int nlen = * len , i ;
char numtmp [ 4 ] ;
for ( source = str , end = str + nlen , target = str ; source < end ; source + + ) {
if ( * source = = ' \\ ' & & source + 1 < end ) {
source + + ;
switch ( * source ) {
case ' n ' : * target + + = ' \n ' ; nlen - - ; break ;
case ' r ' : * target + + = ' \r ' ; nlen - - ; break ;
case ' a ' : * target + + = ' \a ' ; nlen - - ; break ;
case ' t ' : * target + + = ' \t ' ; nlen - - ; break ;
case ' v ' : * target + + = ' \v ' ; nlen - - ; break ;
case ' b ' : * target + + = ' \b ' ; nlen - - ; break ;
case ' f ' : * target + + = ' \f ' ; nlen - - ; break ;
case ' \\ ' : * target + + = ' \\ ' ; nlen - - ; break ;
1999-09-08 00:09:18 +08:00
case ' x ' : if ( source + 1 < end & & isxdigit ( ( int ) ( * ( source + 1 ) ) ) ) {
1999-09-06 04:55:13 +08:00
numtmp [ 0 ] = * + + source ;
1999-09-08 00:09:18 +08:00
if ( source + 1 < end & & isxdigit ( ( int ) ( * ( source + 1 ) ) ) ) {
1999-09-06 04:55:13 +08:00
numtmp [ 1 ] = * + + source ;
numtmp [ 2 ] = ' \0 ' ;
nlen - = 3 ;
} else {
numtmp [ 1 ] = ' \0 ' ;
nlen - = 2 ;
}
1999-09-08 04:29:31 +08:00
* target + + = ( char ) strtol ( numtmp , NULL , 16 ) ;
1999-09-06 04:55:13 +08:00
break ;
}
/* break is left intentionally */
default : i = 0 ;
while ( source < end & & * source > = ' 0 ' & & * source < = ' 7 ' & & i < 3 ) {
numtmp [ i + + ] = * source + + ;
}
if ( i ) {
numtmp [ i ] = ' \0 ' ;
1999-09-08 04:29:31 +08:00
* target + + = ( char ) strtol ( numtmp , NULL , 8 ) ;
1999-09-06 04:55:13 +08:00
nlen - = i ;
source - - ;
} else {
* target + + = ' \\ ' ;
* target + + = * source ;
}
}
} else {
* target + + = * source ;
}
}
* target = ' \0 ' ;
* len = nlen ;
}
PHPAPI char * php_addcslashes ( char * str , int length , int * new_length , int should_free , char * what , int wlength )
1999-09-05 08:55:48 +08:00
{
char flags [ 256 ] ;
1999-09-06 04:55:13 +08:00
char * new_str = emalloc ( ( length ? length : ( length = strlen ( str ) ) ) * 4 + 1 ) ;
1999-09-05 08:55:48 +08:00
char * source , * target ;
char * end ;
char c ;
1999-09-06 04:55:13 +08:00
int newlen ;
1999-09-05 08:55:48 +08:00
if ( ! wlength ) {
wlength = strlen ( what ) ;
}
if ( ! length ) {
length = strlen ( str ) ;
}
memset ( flags , ' \0 ' , sizeof ( flags ) ) ;
for ( source = what , end = source + wlength ; ( c = * source ) | | source < end ; source + + ) {
1999-09-06 04:55:13 +08:00
if ( source + 3 < end & & * ( source + 1 ) = = ' . ' & & * ( source + 2 ) = = ' . ' & & ( unsigned char ) * ( source + 3 ) > = ( unsigned char ) c ) {
memset ( flags + c , 1 , ( unsigned char ) * ( source + 3 ) - ( unsigned char ) c + 1 ) ;
source + = 3 ;
} else
flags [ ( unsigned char ) c ] = 1 ;
1999-09-05 08:55:48 +08:00
}
for ( source = str , end = source + length , target = new_str ; ( c = * source ) | | source < end ; source + + ) {
1999-09-06 04:55:13 +08:00
if ( flags [ ( unsigned char ) c ] ) {
if ( ( unsigned char ) c < 32 | | ( unsigned char ) c > 126 ) {
1999-09-05 08:55:48 +08:00
* target + + = ' \\ ' ;
switch ( c ) {
case ' \n ' : * target + + = ' n ' ; break ;
case ' \t ' : * target + + = ' t ' ; break ;
case ' \r ' : * target + + = ' r ' ; break ;
1999-09-06 04:55:13 +08:00
case ' \a ' : * target + + = ' a ' ; break ;
case ' \v ' : * target + + = ' v ' ; break ;
case ' \b ' : * target + + = ' b ' ; break ;
case ' \f ' : * target + + = ' f ' ; break ;
1999-12-19 22:39:41 +08:00
default : target + = sprintf ( target , " %03o " , ( unsigned char ) c ) ;
1999-09-05 08:55:48 +08:00
}
1999-09-06 04:55:13 +08:00
continue ;
}
* target + + = ' \\ ' ;
}
* target + + = c ;
1999-09-05 08:55:48 +08:00
}
* target = 0 ;
1999-09-06 04:55:13 +08:00
newlen = target - new_str ;
if ( target - new_str < length * 4 ) {
new_str = erealloc ( new_str , newlen + 1 ) ;
}
1999-09-05 08:55:48 +08:00
if ( new_length ) {
1999-09-06 04:55:13 +08:00
* new_length = newlen ;
1999-09-05 08:55:48 +08:00
}
if ( should_free ) {
STR_FREE ( str ) ;
}
return new_str ;
}
1999-04-17 08:37:12 +08:00
1999-09-04 21:18:59 +08:00
PHPAPI char * php_addslashes ( char * str , int length , int * new_length , int should_free )
1999-04-17 08:37:12 +08:00
{
/* maximum string length, worst case situation */
1999-09-06 04:55:13 +08:00
char * new_str = ( char * ) emalloc ( ( length ? length : ( length = strlen ( str ) ) ) * 2 + 1 ) ;
1999-04-17 08:37:12 +08:00
char * source , * target ;
char * end ;
char c ;
1999-06-16 05:51:00 +08:00
PLS_FETCH ( ) ;
1999-09-05 08:55:48 +08:00
1999-04-17 08:37:12 +08:00
for ( source = str , end = source + length , target = new_str ; ( c = * source ) | | source < end ; source + + ) {
switch ( c ) {
case ' \0 ' :
1999-09-06 04:55:13 +08:00
* target + + = ' \\ ' ;
1999-04-17 08:37:12 +08:00
* target + + = ' 0 ' ;
break ;
case ' \' ' :
if ( PG ( magic_quotes_sybase ) ) {
* target + + = ' \' ' ;
* target + + = ' \' ' ;
break ;
}
/* break is missing *intentionally* */
case ' \" ' :
case ' \\ ' :
if ( ! PG ( magic_quotes_sybase ) ) {
* target + + = ' \\ ' ;
}
/* break is missing *intentionally* */
default :
* target + + = c ;
break ;
}
}
* target = 0 ;
1999-06-16 05:51:00 +08:00
if ( new_length ) {
* new_length = target - new_str ;
}
1999-04-17 08:37:12 +08:00
if ( should_free ) {
STR_FREE ( str ) ;
}
return new_str ;
}
# define _HEB_BLOCK_TYPE_ENG 1
# define _HEB_BLOCK_TYPE_HEB 2
# define isheb(c) (((((unsigned char) c)>=224) && (((unsigned char) c)<=250)) ? 1 : 0)
# define _isblank(c) (((((unsigned char) c)==' ' || ((unsigned char) c)=='\t')) ? 1 : 0)
# define _isnewline(c) (((((unsigned char) c)=='\n' || ((unsigned char) c)=='\r')) ? 1 : 0)
1999-10-16 04:04:31 +08:00
PHPAPI void php_char_to_str ( char * str , uint len , char from , char * to , int to_len , pval * result )
1999-04-17 08:37:12 +08:00
{
int char_count = 0 ;
char * source , * target , * tmp , * source_end = str + len , * tmp_end = NULL ;
for ( source = str ; source < source_end ; source + + ) {
if ( * source = = from ) {
char_count + + ;
}
}
result - > type = IS_STRING ;
if ( char_count = = 0 ) {
result - > value . str . val = estrndup ( str , len ) ;
result - > value . str . len = len ;
return ;
}
result - > value . str . len = len + char_count * ( to_len - 1 ) ;
result - > value . str . val = target = ( char * ) emalloc ( result - > value . str . len + 1 ) ;
for ( source = str ; source < source_end ; source + + ) {
if ( * source = = from ) {
for ( tmp = to , tmp_end = tmp + to_len ; tmp < tmp_end ; tmp + + ) {
* target = * tmp ;
target + + ;
}
} else {
* target = * source ;
target + + ;
}
}
* target = 0 ;
}
1999-12-12 03:50:01 +08:00
PHPAPI inline char *
1999-10-17 00:35:20 +08:00
php_memnstr ( char * haystack , char * needle , int needle_len , char * end )
1999-04-17 08:37:12 +08:00
{
char * p = haystack ;
char * s = NULL ;
1999-05-12 23:40:15 +08:00
for ( ; p < = end - needle_len & &
( s = memchr ( p , * needle , end - p - needle_len + 1 ) ) ; p = s + 1 ) {
1999-04-17 08:37:12 +08:00
if ( memcmp ( s , needle , needle_len ) = = 0 )
return s ;
}
return NULL ;
}
1999-10-16 04:04:31 +08:00
PHPAPI char * php_str_to_str ( char * haystack , int length ,
1999-04-17 08:37:12 +08:00
char * needle , int needle_len , char * str , int str_len , int * _new_length )
{
char * p , * q ;
char * r , * s ;
char * end = haystack + length ;
char * new ;
1999-05-12 23:40:15 +08:00
char * off ;
1999-04-17 08:37:12 +08:00
1999-05-12 23:40:15 +08:00
new = emalloc ( length ) ;
1999-04-17 08:37:12 +08:00
/* we jump through haystack searching for the needle. hurray! */
for ( p = haystack , q = new ;
1999-10-17 00:35:20 +08:00
( r = php_memnstr ( p , needle , needle_len , end ) ) ; ) {
1999-04-17 08:37:12 +08:00
/* this ain't optimal. you could call it `efficient memory usage' */
1999-05-12 23:40:15 +08:00
off = erealloc ( new , ( q - new ) + ( r - p ) + ( str_len ) + 1 ) ;
if ( off ! = new ) {
if ( ! off ) {
goto finish ;
}
q + = off - new ;
new = off ;
}
1999-04-17 08:37:12 +08:00
memcpy ( q , p , r - p ) ;
q + = r - p ;
memcpy ( q , str , str_len ) ;
q + = str_len ;
p = r + needle_len ;
}
1999-05-12 23:40:15 +08:00
1999-04-17 08:37:12 +08:00
/* if there is a rest, copy it */
1999-05-12 23:40:15 +08:00
if ( ( end - p ) > 0 ) {
1999-04-17 08:37:12 +08:00
s = ( q ) + ( end - p ) ;
1999-05-12 23:40:15 +08:00
off = erealloc ( new , s - new + 1 ) ;
if ( off ! = new ) {
if ( ! off ) {
goto finish ;
}
q + = off - new ;
new = off ;
s = q + ( end - p ) ;
}
1999-04-17 08:37:12 +08:00
memcpy ( q , p , end - p ) ;
q = s ;
}
1999-05-12 23:40:15 +08:00
finish :
1999-04-17 08:37:12 +08:00
* q = ' \0 ' ;
if ( _new_length ) * _new_length = q - new ;
return new ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-04-17 08:37:12 +08:00
/* {{{ proto string str_replace(string needle, string str, string haystack)
Replace all occurrences of needle in haystack with str */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( str_replace )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * haystack , * * needle , * * str ;
1999-04-17 08:37:12 +08:00
char * new ;
1999-05-12 23:40:15 +08:00
int len = 0 ;
1999-04-17 08:37:12 +08:00
1999-06-16 05:51:00 +08:00
if ( ARG_COUNT ( ht ) ! = 3 | |
1999-12-19 06:40:35 +08:00
zend_get_parameters_ex ( 3 , & needle , & str , & haystack ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( haystack ) ;
convert_to_string_ex ( needle ) ;
convert_to_string_ex ( str ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * haystack ) - > value . str . len = = 0 ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
RETURN_STRING ( empty_string , 1 ) ;
}
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > value . str . len = = 1 ) {
1999-10-16 04:04:31 +08:00
php_char_to_str ( ( * haystack ) - > value . str . val ,
( * haystack ) - > value . str . len ,
( * needle ) - > value . str . val [ 0 ] ,
( * str ) - > value . str . val ,
( * str ) - > value . str . len ,
return_value ) ;
1999-04-17 08:37:12 +08:00
return ;
}
1999-09-25 19:55:42 +08:00
if ( ( * needle ) - > value . str . len = = 0 ) {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " The length of the needle must not be 0 " ) ;
1999-05-12 23:40:15 +08:00
RETURN_FALSE ;
}
1999-10-16 04:04:31 +08:00
new = php_str_to_str ( ( * haystack ) - > value . str . val , ( * haystack ) - > value . str . len ,
( * needle ) - > value . str . val , ( * needle ) - > value . str . len ,
( * str ) - > value . str . val , ( * str ) - > value . str . len , & len ) ;
1999-05-12 23:40:15 +08:00
RETURN_STRINGL ( new , len , 0 ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* Converts Logical Hebrew text (Hebrew Windows style) to Visual text
* Cheers / complaints / flames - Zeev Suraski < zeev @ php . net >
*/
1999-10-17 00:35:20 +08:00
static void php_hebrev ( INTERNAL_FUNCTION_PARAMETERS , int convert_newlines )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str , * * max_chars_per_line ;
1999-04-17 08:37:12 +08:00
char * heb_str , * tmp , * target , * opposite_target , * broken_str ;
int block_start , block_end , block_type , block_length , i ;
int block_ended ;
long max_chars = 0 ;
int begin , end , char_count , orig_begin ;
switch ( ARG_COUNT ( ht ) ) {
case 1 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
break ;
case 2 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 2 , & str , & max_chars_per_line ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
convert_to_long_ex ( max_chars_per_line ) ;
max_chars = ( * max_chars_per_line ) - > value . lval ;
1999-04-17 08:37:12 +08:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
if ( ( * str ) - > value . str . len = = 0 ) {
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
tmp = ( * str ) - > value . str . val ;
1999-04-17 08:37:12 +08:00
block_start = block_end = 0 ;
block_ended = 0 ;
1999-09-25 19:55:42 +08:00
heb_str = ( char * ) emalloc ( ( * str ) - > value . str . len + 1 ) ;
target = heb_str + ( * str ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
opposite_target = heb_str ;
* target = 0 ;
target - - ;
block_length = 0 ;
if ( isheb ( * tmp ) ) {
block_type = _HEB_BLOCK_TYPE_HEB ;
} else {
block_type = _HEB_BLOCK_TYPE_ENG ;
}
do {
if ( block_type = = _HEB_BLOCK_TYPE_HEB ) {
1999-09-25 19:55:42 +08:00
while ( ( isheb ( ( int ) * ( tmp + 1 ) ) | | _isblank ( ( int ) * ( tmp + 1 ) ) | | ispunct ( ( int ) * ( tmp + 1 ) ) | | ( int ) * ( tmp + 1 ) = = ' \n ' ) & & block_end < ( * str ) - > value . str . len - 1 ) {
1999-04-17 08:37:12 +08:00
tmp + + ;
block_end + + ;
block_length + + ;
}
for ( i = block_start ; i < = block_end ; i + + ) {
1999-09-25 19:55:42 +08:00
* target = ( * str ) - > value . str . val [ i ] ;
1999-04-17 08:37:12 +08:00
switch ( * target ) {
case ' ( ' :
* target = ' ) ' ;
break ;
case ' ) ' :
* target = ' ( ' ;
break ;
default :
break ;
}
target - - ;
}
block_type = _HEB_BLOCK_TYPE_ENG ;
} else {
1999-09-25 19:55:42 +08:00
while ( ! isheb ( * ( tmp + 1 ) ) & & ( int ) * ( tmp + 1 ) ! = ' \n ' & & block_end < ( * str ) - > value . str . len - 1 ) {
1999-04-17 08:37:12 +08:00
tmp + + ;
block_end + + ;
block_length + + ;
}
while ( ( _isblank ( ( int ) * tmp ) | | ispunct ( ( int ) * tmp ) ) & & * tmp ! = ' / ' & & * tmp ! = ' - ' & & block_end > block_start ) {
tmp - - ;
block_end - - ;
}
for ( i = block_end ; i > = block_start ; i - - ) {
1999-09-25 19:55:42 +08:00
* target = ( * str ) - > value . str . val [ i ] ;
1999-04-17 08:37:12 +08:00
target - - ;
}
block_type = _HEB_BLOCK_TYPE_HEB ;
}
block_start = block_end + 1 ;
1999-09-25 19:55:42 +08:00
} while ( block_end < ( * str ) - > value . str . len - 1 ) ;
1999-04-17 08:37:12 +08:00
1999-09-25 19:55:42 +08:00
broken_str = ( char * ) emalloc ( ( * str ) - > value . str . len + 1 ) ;
begin = end = ( * str ) - > value . str . len - 1 ;
1999-04-17 08:37:12 +08:00
target = broken_str ;
while ( 1 ) {
char_count = 0 ;
while ( ( ! max_chars | | char_count < max_chars ) & & begin > 0 ) {
char_count + + ;
begin - - ;
if ( begin < = 0 | | _isnewline ( heb_str [ begin ] ) ) {
while ( begin > 0 & & _isnewline ( heb_str [ begin - 1 ] ) ) {
begin - - ;
char_count + + ;
}
break ;
}
}
if ( char_count = = max_chars ) { /* try to avoid breaking words */
int new_char_count = char_count , new_begin = begin ;
while ( new_char_count > 0 ) {
if ( _isblank ( heb_str [ new_begin ] ) | | _isnewline ( heb_str [ new_begin ] ) ) {
break ;
}
new_begin + + ;
new_char_count - - ;
}
if ( new_char_count > 0 ) {
char_count = new_char_count ;
begin = new_begin ;
}
}
orig_begin = begin ;
if ( _isblank ( heb_str [ begin ] ) ) {
heb_str [ begin ] = ' \n ' ;
}
while ( begin < = end & & _isnewline ( heb_str [ begin ] ) ) { /* skip leading newlines */
begin + + ;
}
for ( i = begin ; i < = end ; i + + ) { /* copy content */
* target = heb_str [ i ] ;
target + + ;
}
for ( i = orig_begin ; i < = end & & _isnewline ( heb_str [ i ] ) ; i + + ) {
* target = heb_str [ i ] ;
target + + ;
}
begin = orig_begin ;
if ( begin < = 0 ) {
* target = 0 ;
break ;
}
begin - - ;
end = begin ;
}
efree ( heb_str ) ;
if ( convert_newlines ) {
1999-10-16 04:04:31 +08:00
php_char_to_str ( broken_str , ( * str ) - > value . str . len , ' \n ' , " <br> \n " , 5 , return_value ) ;
1999-04-17 08:37:12 +08:00
efree ( broken_str ) ;
} else {
return_value - > value . str . val = broken_str ;
1999-09-25 19:55:42 +08:00
return_value - > value . str . len = ( * str ) - > value . str . len ;
1999-04-17 08:37:12 +08:00
return_value - > type = IS_STRING ;
}
}
/* {{{ proto string hebrev(string str [, int max_chars_per_line])
Convert logical Hebrew text to visual text */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( hebrev )
1999-04-17 08:37:12 +08:00
{
1999-10-17 00:35:20 +08:00
php_hebrev ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* {{{ proto string hebrevc(string str [, int max_chars_per_line])
1999-04-17 08:37:12 +08:00
Convert logical Hebrew text to visual text with newline conversion */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
PHP_FUNCTION ( hebrevc )
1999-04-17 08:37:12 +08:00
{
1999-10-17 00:35:20 +08:00
php_hebrev ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
/* {{{ proto string nl2br(string str)
Converts newlines to HTML line breaks */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
PHP_FUNCTION ( nl2br )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * str ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
1999-04-17 08:37:12 +08:00
1999-10-16 04:04:31 +08:00
php_char_to_str ( ( * str ) - > value . str . val , ( * str ) - > value . str . len , ' \n ' , " <br> \n " , 5 , return_value ) ;
1999-04-17 08:37:12 +08:00
}
/* }}} */
1999-09-21 04:05:26 +08:00
/* {{{ proto string strip_tags(string str [, string allowable_tags])
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
Strips HTML and PHP tags from a string */
PHP_FUNCTION ( strip_tags )
{
char * buf ;
1999-09-25 19:55:42 +08:00
pval * * str , * * allow = NULL ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-09-20 23:50:56 +08:00
switch ( ARG_COUNT ( ht ) ) {
case 1 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-09-20 23:50:56 +08:00
RETURN_FALSE ;
}
break ;
case 2 :
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 2 , & str , & allow ) = = FAILURE ) {
1999-09-20 23:50:56 +08:00
RETURN_FALSE ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( allow ) ;
1999-09-20 23:50:56 +08:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( str ) ;
buf = estrdup ( ( * str ) - > value . str . val ) ;
1999-10-17 00:35:20 +08:00
php_strip_tags ( buf , ( * str ) - > value . str . len , 0 , allow ? ( * allow ) - > value . str . val : NULL ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
RETURN_STRING ( buf , 0 ) ;
}
/* }}} */
1999-04-17 08:37:12 +08:00
/* {{{ proto string setlocale(string category, string locale)
Set locale information */
1999-06-16 05:51:00 +08:00
PHP_FUNCTION ( setlocale )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * pcategory , * * plocale ;
1999-04-17 08:37:12 +08:00
pval * category , * locale ;
int cat ;
char * loc , * retval ;
1999-11-28 08:31:02 +08:00
BLS_FETCH ( ) ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & pcategory , & plocale ) = = FAILURE )
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
# ifdef HAVE_SETLOCALE
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( pcategory ) ;
convert_to_string_ex ( plocale ) ;
category = * pcategory ;
locale = * plocale ;
1999-04-17 08:37:12 +08:00
if ( ! strcasecmp ( " LC_ALL " , category - > value . str . val ) )
cat = LC_ALL ;
else if ( ! strcasecmp ( " LC_COLLATE " , category - > value . str . val ) )
cat = LC_COLLATE ;
else if ( ! strcasecmp ( " LC_CTYPE " , category - > value . str . val ) )
cat = LC_CTYPE ;
else if ( ! strcasecmp ( " LC_MONETARY " , category - > value . str . val ) )
cat = LC_MONETARY ;
else if ( ! strcasecmp ( " LC_NUMERIC " , category - > value . str . val ) )
cat = LC_NUMERIC ;
else if ( ! strcasecmp ( " LC_TIME " , category - > value . str . val ) )
cat = LC_TIME ;
else {
1999-08-03 03:17:14 +08:00
php_error ( E_WARNING , " Invalid locale category name %s, must be one of LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC or LC_TIME " , category - > value . str . val ) ;
1999-04-17 08:37:12 +08:00
RETURN_FALSE ;
}
if ( ! strcmp ( " 0 " , locale - > value . str . val ) )
loc = NULL ;
else
loc = locale - > value . str . val ;
retval = setlocale ( cat , loc ) ;
if ( retval ) {
1999-10-08 10:10:57 +08:00
/* Remember if locale was changed */
if ( loc ) {
1999-11-28 08:31:02 +08:00
STR_FREE ( BG ( locale_string ) ) ;
BG ( strtok_string ) = estrdup ( retval ) ;
1999-10-08 10:10:57 +08:00
}
1999-04-17 08:37:12 +08:00
RETVAL_STRING ( retval , 1 ) ;
return ;
}
# endif
RETURN_FALSE ;
}
/* }}} */
1999-07-25 06:16:54 +08:00
/* {{{ proto void parse_str(string encoded_string)
1999-04-17 08:37:12 +08:00
Parses GET / POST / COOKIE data and sets global variables . */
1999-07-25 06:16:54 +08:00
PHP_FUNCTION ( parse_str )
1999-04-17 08:37:12 +08:00
{
1999-09-25 19:55:42 +08:00
pval * * arg ;
1999-04-17 08:37:12 +08:00
char * res = NULL ;
1999-09-11 22:09:29 +08:00
ELS_FETCH ( ) ;
PLS_FETCH ( ) ;
SLS_FETCH ( ) ;
1999-04-17 08:37:12 +08:00
1999-12-19 06:40:35 +08:00
if ( zend_get_parameters_ex ( 1 , & arg ) = = FAILURE ) {
1999-04-17 08:37:12 +08:00
WRONG_PARAM_COUNT ;
}
1999-09-25 19:55:42 +08:00
convert_to_string_ex ( arg ) ;
if ( ( * arg ) - > value . str . val & & * ( * arg ) - > value . str . val ) {
res = estrndup ( ( * arg ) - > value . str . val , ( * arg ) - > value . str . len ) ;
1999-04-17 08:37:12 +08:00
}
1999-09-11 22:09:29 +08:00
php_treat_data ( PARSE_STRING , res ELS_CC PLS_CC SLS_CC ) ;
1999-04-17 08:37:12 +08:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* }}} */
1999-09-20 23:50:56 +08:00
# define PHP_TAG_BUF_SIZE 1023
/* Check if tag is in a set of tags
*
* states :
*
* 0 start tag
* 1 first non - whitespace char seen
*/
int php_tag_find ( char * tag , int len , char * set ) {
char c , * n , * t ;
int i = 0 , state = 0 , done = 0 ;
char * norm = emalloc ( len ) ;
n = norm ;
t = tag ;
c = tolower ( * t ) ;
/*
normalize the tag removing leading and trailing whitespace
and turn any < a whatever . . . > into just < a > and any < / tag >
into < tag >
*/
while ( i < len & & ! done ) {
switch ( c ) {
case ' < ' :
* ( n + + ) = c ;
break ;
case ' > ' :
done = 1 ;
break ;
default :
1999-09-22 01:06:30 +08:00
if ( ! isspace ( ( int ) c ) ) {
1999-09-20 23:50:56 +08:00
if ( state = = 0 ) {
state = 1 ;
if ( c ! = ' / ' ) * ( n + + ) = c ;
} else {
* ( n + + ) = c ;
}
} else {
if ( state = = 1 ) done = 1 ;
}
break ;
}
c = tolower ( * ( + + t ) ) ;
}
* ( n + + ) = ' > ' ;
* n = ' \0 ' ;
if ( strstr ( set , norm ) ) done = 1 ;
else done = 0 ;
efree ( norm ) ;
return done ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
/* A simple little state-machine to strip out html and php tags
State 0 is the output state , State 1 means we are inside a
normal html tag and state 2 means we are inside a php tag .
The state variable is passed in to allow a function like fgetss
to maintain state across calls to the function .
lc holds the last significant character read and br is a bracket
counter .
1999-09-20 23:50:56 +08:00
When an allow string is passed in we keep track of the string
in state 1 and when the tag is closed check it against the
allow string to see if we should allow it .
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
*/
1999-10-17 00:35:20 +08:00
PHPAPI void php_strip_tags ( char * rbuf , int len , int state , char * allow ) {
1999-09-20 23:50:56 +08:00
char * tbuf , * buf , * p , * tp , * rp , c , lc ;
int br , i = 0 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
buf = estrdup ( rbuf ) ;
c = * buf ;
lc = ' \0 ' ;
p = buf ;
rp = rbuf ;
br = 0 ;
1999-09-20 23:50:56 +08:00
if ( allow ) {
1999-12-10 12:44:32 +08:00
php_strtolower ( allow , len ) ;
1999-09-20 23:50:56 +08:00
tbuf = emalloc ( PHP_TAG_BUF_SIZE + 1 ) ;
tp = tbuf ;
1999-09-24 23:34:54 +08:00
} else {
tbuf = tp = NULL ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
1999-09-20 23:50:56 +08:00
while ( i < len ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
switch ( c ) {
case ' < ' :
if ( state = = 0 ) {
lc = ' < ' ;
state = 1 ;
1999-09-20 23:50:56 +08:00
if ( allow ) {
* ( tp + + ) = ' < ' ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
break ;
case ' ( ' :
if ( state = = 2 ) {
if ( lc ! = ' \" ' ) {
lc = ' ( ' ;
br + + ;
}
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
}
break ;
case ' ) ' :
if ( state = = 2 ) {
if ( lc ! = ' \" ' ) {
lc = ' ) ' ;
br - - ;
}
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
}
break ;
case ' > ' :
if ( state = = 1 ) {
lc = ' > ' ;
state = 0 ;
1999-09-20 23:50:56 +08:00
if ( allow ) {
* ( tp + + ) = ' > ' ;
* tp = ' \0 ' ;
if ( php_tag_find ( tbuf , tp - tbuf , allow ) ) {
memcpy ( rp , tbuf , tp - tbuf ) ;
rp + = tp - tbuf ;
}
tp = tbuf ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
} else if ( state = = 2 ) {
if ( ! br & & lc ! = ' \" ' & & * ( p - 1 ) = = ' ? ' ) {
state = 0 ;
}
}
break ;
case ' \" ' :
if ( state = = 2 ) {
if ( lc = = ' \" ' ) {
lc = ' \0 ' ;
} else if ( lc ! = ' \\ ' ) {
lc = ' \" ' ;
}
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
1999-09-20 23:50:56 +08:00
} else if ( allow & & state = = 1 ) {
* ( tp + + ) = c ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
break ;
case ' ? ' :
if ( state = = 1 & & * ( p - 1 ) = = ' < ' ) {
br = 0 ;
state = 2 ;
break ;
}
/* fall-through */
default :
if ( state = = 0 ) {
* ( rp + + ) = c ;
1999-09-20 23:50:56 +08:00
} else if ( allow & & state = = 1 ) {
* ( tp + + ) = c ;
if ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ) { /* no buffer overflows */
tp = tbuf ;
}
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
break ;
}
c = * ( + + p ) ;
1999-09-20 23:50:56 +08:00
i + + ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
* rp = ' \0 ' ;
efree ( buf ) ;
1999-09-20 23:50:56 +08:00
if ( allow ) efree ( tbuf ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
}
1999-04-17 08:37:12 +08:00
1999-10-28 06:06:05 +08:00
/* {{{ proto string str_repeat(string input, int mult)
Returns the input string repeat mult times */
PHP_FUNCTION ( str_repeat )
{
zval * * input_str ; /* Input string */
zval * * mult ; /* Multiplier */
char * result ; /* Resulting string */
int result_len ; /* Length of the resulting string */
int i ;
1999-12-19 06:40:35 +08:00
if ( ARG_COUNT ( ht ) ! = 2 | | zend_get_parameters_ex ( 2 , & input_str , & mult ) = = FAILURE ) {
1999-10-28 06:06:05 +08:00
WRONG_PARAM_COUNT ;
}
/* Make sure we're dealing with proper types */
convert_to_string_ex ( input_str ) ;
convert_to_long_ex ( mult ) ;
if ( ( * mult ) - > value . lval < 1 ) {
php_error ( E_WARNING , " Second argument to %s() has to be greater than 0 " ,
get_active_function_name ( ) ) ;
return ;
}
/* Don't waste our time if it's empty */
if ( ( * input_str ) - > value . str . len = = 0 )
RETURN_STRINGL ( empty_string , 0 , 1 ) ;
/* Initialize the result string */
result_len = ( * input_str ) - > value . str . len * ( * mult ) - > value . lval ;
result = ( char * ) emalloc ( result_len + 1 ) ;
/* Copy the input string into the result as many times as necessary */
for ( i = 0 ; i < ( * mult ) - > value . lval ; i + + ) {
memcpy ( result + ( * input_str ) - > value . str . len * i ,
( * input_str ) - > value . str . val ,
( * input_str ) - > value . str . len ) ;
}
result [ result_len ] = ' \0 ' ;
RETURN_STRINGL ( result , result_len + 1 , 0 ) ;
}
/* }}} */
1999-12-14 11:52:12 +08:00
/* {{{ proto mixed count_chars(string input[, int mode])
Returns info about what characters are used in input */
PHP_FUNCTION ( count_chars )
{
zval * * input , * * mode ;
int chars [ 256 ] ;
int ac = ARG_COUNT ( ht ) ;
int mymode = 0 ;
unsigned char * buf ;
int len , inx ;
char retstr [ 256 ] ;
int retlen = 0 ;
1999-12-19 06:40:35 +08:00
if ( ac < 1 | | ac > 2 | | zend_get_parameters_ex ( ac , & input , & mode ) = = FAILURE ) {
1999-12-14 11:52:12 +08:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( input ) ;
if ( ac = = 2 ) {
convert_to_long_ex ( mode ) ;
mymode = ( * mode ) - > value . lval ;
if ( mymode < 0 | | mymode > 4 ) {
php_error ( E_WARNING , " unknown mode " ) ;
RETURN_FALSE ;
}
}
len = ( * input ) - > value . str . len ;
buf = ( unsigned char * ) ( * input ) - > value . str . val ;
memset ( ( void * ) chars , 0 , sizeof ( chars ) ) ;
while ( len > 0 ) {
chars [ * buf ] + + ;
buf + + ;
len - - ;
}
if ( mymode < 3 ) {
array_init ( return_value ) ;
}
for ( inx = 0 ; inx < 255 ; inx + + ) {
switch ( mymode ) {
case 0 :
add_index_long ( return_value , inx , chars [ inx ] ) ;
break ;
case 1 :
if ( chars [ inx ] ! = 0 ) {
add_index_long ( return_value , inx , chars [ inx ] ) ;
}
break ;
case 2 :
if ( chars [ inx ] = = 0 ) {
add_index_long ( return_value , inx , chars [ inx ] ) ;
}
break ;
case 3 :
if ( chars [ inx ] ! = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
case 4 :
if ( chars [ inx ] = = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
}
}
if ( mymode > = 3 & & mymode < = 4 ) {
RETURN_STRINGL ( retstr , retlen , 1 ) ;
}
}
/* }}} */