Skip to content

Commit b1e9c73

Browse files
Andreas Treichelnikic
Andreas Treichel
authored andcommitted
Allow strip_tags with an array of allowed tagnames
1 parent bf774d4 commit b1e9c73

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ PHP 7.4 UPGRADE NOTES
121121
check whether this statement is read-only, i.e. whether it doesn't modify
122122
the database.
123123

124+
- Standard:
125+
. strip_tags() now also accepts an array of allowed tags: Instead of
126+
strip_tags($str, '<a><p>') you can now write strip_tags($str, ['a', 'p']).
127+
124128
========================================
125129
3. Changes in SAPI modules
126130
========================================

ext/standard/string.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,22 +4780,42 @@ PHP_FUNCTION(strip_tags)
47804780
zval *allow=NULL;
47814781
const char *allowed_tags=NULL;
47824782
size_t allowed_tags_len=0;
4783+
smart_str tags_ss = {0};
47834784

47844785
ZEND_PARSE_PARAMETERS_START(1, 2)
47854786
Z_PARAM_STR(str)
47864787
Z_PARAM_OPTIONAL
47874788
Z_PARAM_ZVAL(allow)
47884789
ZEND_PARSE_PARAMETERS_END();
47894790

4790-
/* To maintain a certain BC, we allow anything for the second parameter and return original string */
47914791
if (allow) {
4792-
convert_to_string(allow);
4793-
allowed_tags = Z_STRVAL_P(allow);
4794-
allowed_tags_len = Z_STRLEN_P(allow);
4792+
if (Z_TYPE_P(allow) == IS_ARRAY) {
4793+
zval *tmp;
4794+
zend_string *tag;
4795+
4796+
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(allow), tmp) {
4797+
tag = zval_get_string(tmp);
4798+
smart_str_appendc(&tags_ss, '<');
4799+
smart_str_append(&tags_ss, tag);
4800+
smart_str_appendc(&tags_ss, '>');
4801+
zend_string_release(tag);
4802+
} ZEND_HASH_FOREACH_END();
4803+
if (tags_ss.s) {
4804+
smart_str_0(&tags_ss);
4805+
allowed_tags = ZSTR_VAL(tags_ss.s);
4806+
allowed_tags_len = ZSTR_LEN(tags_ss.s);
4807+
}
4808+
} else {
4809+
/* To maintain a certain BC, we allow anything for the second parameter and return original string */
4810+
convert_to_string(allow);
4811+
allowed_tags = Z_STRVAL_P(allow);
4812+
allowed_tags_len = Z_STRLEN_P(allow);
4813+
}
47954814
}
47964815

47974816
buf = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
47984817
ZSTR_LEN(buf) = php_strip_tags_ex(ZSTR_VAL(buf), ZSTR_LEN(str), NULL, allowed_tags, allowed_tags_len, 0);
4818+
smart_str_free(&tags_ss);
47994819
RETURN_NEW_STR(buf);
48004820
}
48014821
/* }}} */
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Test strip_tags() function : basic functionality - with array argument
3+
--FILE--
4+
<?php
5+
6+
$string = '<p>foo <b>bar</b> <a href="#">foobar</a></p>';
7+
var_dump(strip_tags($string));
8+
var_dump(strip_tags($string, ['a']));
9+
var_dump(strip_tags($string, ['p', 'a']));
10+
var_dump(strip_tags($string, []));
11+
var_dump(strip_tags($string, ['p' => true, 'a' => false]));
12+
var_dump(strip_tags($string, ['p' => 'a']));
13+
14+
// Previous tests from strip_tags_variation2.phpt
15+
var_dump(strip_tags($string, [0]));
16+
var_dump(strip_tags($string, [1]));
17+
var_dump(strip_tags($string, [1, 2]));
18+
var_dump(strip_tags($string, ['color' => 'red', 'item' => 'pen']));
19+
echo "Done";
20+
?>
21+
--EXPECT--
22+
string(14) "foo bar foobar"
23+
string(30) "foo bar <a href="#">foobar</a>"
24+
string(37) "<p>foo bar <a href="#">foobar</a></p>"
25+
string(14) "foo bar foobar"
26+
string(14) "foo bar foobar"
27+
string(30) "foo bar <a href="#">foobar</a>"
28+
string(14) "foo bar foobar"
29+
string(14) "foo bar foobar"
30+
string(14) "foo bar foobar"
31+
string(14) "foo bar foobar"
32+
Done

ext/standard/tests/strings/strip_tags_variation2.phpt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ $values = array(
4848
10.6E-10,
4949
.5,
5050

51-
// array data
52-
array(),
53-
array(0),
54-
array(1),
55-
array(1, 2),
56-
array('color' => 'red', 'item' => 'pen'),
57-
5851
// null data
5952
NULL,
6053
null,
@@ -113,24 +106,14 @@ string(10) "helloworld"
113106
-- Iteration 9 --
114107
string(10) "helloworld"
115108
-- Iteration 10 --
116-
117-
Notice: Array to string conversion in %s on line %d
118109
string(10) "helloworld"
119110
-- Iteration 11 --
120-
121-
Notice: Array to string conversion in %s on line %d
122111
string(10) "helloworld"
123112
-- Iteration 12 --
124-
125-
Notice: Array to string conversion in %s on line %d
126113
string(10) "helloworld"
127114
-- Iteration 13 --
128-
129-
Notice: Array to string conversion in %s on line %d
130115
string(10) "helloworld"
131116
-- Iteration 14 --
132-
133-
Notice: Array to string conversion in %s on line %d
134117
string(10) "helloworld"
135118
-- Iteration 15 --
136119
string(10) "helloworld"
@@ -146,14 +129,4 @@ string(10) "helloworld"
146129
string(10) "helloworld"
147130
-- Iteration 21 --
148131
string(10) "helloworld"
149-
-- Iteration 22 --
150-
string(10) "helloworld"
151-
-- Iteration 23 --
152-
string(10) "helloworld"
153-
-- Iteration 24 --
154-
string(10) "helloworld"
155-
-- Iteration 25 --
156-
string(10) "helloworld"
157-
-- Iteration 26 --
158-
string(10) "helloworld"
159132
Done

0 commit comments

Comments
 (0)