php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60630
Patch str_begins_ends_with revision 2011-12-31 12:15 UTC by regality at gmail dot com

Patch str_begins_ends_with for Strings related Bug #60630

Patch version 2011-12-31 12:15 UTC

Return to Bug #60630 | Download this patch
Patch Revisions:

Developer: regality@gmail.com

Index: ext/standard/string.c
===================================================================
--- ext/standard/string.c	(revision 321605)
+++ ext/standard/string.c	(working copy)
@@ -1784,6 +1784,64 @@
    An alias for strstr */
 /* }}} */
 
+/* {{{ proto bool str_begins_with(string haystack, string needle [, bool case_insensitive])
+   Checks if haystack begins with needle */
+PHP_FUNCTION(str_begins_with)
+{
+	char *haystack;
+	char *needle;
+	int   haystack_len;
+	int   needle_len;
+	zend_bool ci = 0;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &haystack, &haystack_len, &needle, &needle_len, &ci) == FAILURE) {
+		RETURN_NULL();
+	} else if (haystack_len < needle_len) {
+		RETURN_FALSE;
+	} else {
+		haystack[needle_len] = 0; // only test to the length of needle
+		if (ci) {
+			php_strtolower(haystack, needle_len);
+			php_strtolower(needle, needle_len);
+		}
+		if (strcmp(haystack, needle) == 0) {
+			RETURN_TRUE;
+		} else {
+			RETURN_FALSE;
+		}
+	}
+}
+/* }}} */
+
+/* {{{ proto bool str_ends_with(string haystack, string needle [, case_insensitive])
+   Checks if haystack ends with needle */
+PHP_FUNCTION(str_ends_with)
+{
+	char *haystack;
+	char *needle;
+	int   haystack_len;
+	int   needle_len;
+	zend_bool ci = 0;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &haystack, &haystack_len, &needle, &needle_len, &ci) == FAILURE) {
+		RETURN_NULL();
+	} else if (haystack_len < needle_len) {
+		RETURN_FALSE;
+	} else {
+		haystack += sizeof(char) * (haystack_len - needle_len); // chop the beginning of the string
+		if (ci) {
+			php_strtolower(haystack, needle_len);
+			php_strtolower(needle, needle_len);
+		}
+		if (strcmp(haystack, needle) == 0) {
+			RETURN_TRUE;
+		} else {
+			RETURN_FALSE;
+		}
+	}
+}
+/* }}} */
+
 /* {{{ proto int strpos(string haystack, string needle [, int offset])
    Finds position of first occurrence of a string within another */
 PHP_FUNCTION(strpos)
Index: ext/standard/basic_functions.c
===================================================================
--- ext/standard/basic_functions.c	(revision 321605)
+++ ext/standard/basic_functions.c	(working copy)
@@ -2241,6 +2241,18 @@
 	ZEND_ARG_INFO(0, part)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_str_begins_with, 0, 0, 2)
+	ZEND_ARG_INFO(0, haystack)
+	ZEND_ARG_INFO(0, needle)
+	ZEND_ARG_INFO(0, case_insensitive)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_str_ends_with, 0, 0, 2)
+	ZEND_ARG_INFO(0, haystack)
+	ZEND_ARG_INFO(0, needle)
+	ZEND_ARG_INFO(0, case_insensitive)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_strpos, 0, 0, 2)
 	ZEND_ARG_INFO(0, haystack)
 	ZEND_ARG_INFO(0, needle)
@@ -2734,6 +2746,8 @@
 	PHP_FE(strtok,															arginfo_strtok)
 	PHP_FE(strtoupper,														arginfo_strtoupper)
 	PHP_FE(strtolower,														arginfo_strtolower)
+	PHP_FE(str_begins_with,												arginfo_str_begins_with)
+	PHP_FE(str_ends_with,												arginfo_str_ends_with)
 	PHP_FE(strpos,															arginfo_strpos)
 	PHP_FE(stripos,															arginfo_stripos)
 	PHP_FE(strrpos,															arginfo_strrpos)
Index: ext/standard/php_string.h
===================================================================
--- ext/standard/php_string.h	(revision 321605)
+++ ext/standard/php_string.h	(working copy)
@@ -45,6 +45,8 @@
 PHP_FUNCTION(dirname);
 PHP_FUNCTION(pathinfo);
 PHP_FUNCTION(strstr);
+PHP_FUNCTION(str_begins_with);
+PHP_FUNCTION(str_ends_with);
 PHP_FUNCTION(strpos);
 PHP_FUNCTION(stripos);
 PHP_FUNCTION(strrpos);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 14:01:29 2024 UTC