What is stripslashes in php

stripslashes

Замечание:

Если включена директива magic_quotes_sybase, вместо обратных слешей будут удаляться двойные одинарные кавычки.

Функцию stripslashes() можно использовать, например, если директива конфигурации magic_quotes_gpc имеет значение on (она была включена по умолчанию в версиях до PHP 5.4), и экранирование символов не требуется. Например, данные не вставляются в базу данных, а просто выводятся в браузер.

Список параметров

Возвращаемые значения

Возвращает строку с вырезанными обратными слешами. (\’ становится и т.п.) Двойные обратные слеши (\\) становятся одинарными (\).

Примеры

Пример #1 Пример использования stripslashes()

// выводит: Вас зовут O’reilly?
echo stripslashes ( $str );
?>

Замечание:

stripslashes() не рекурсивна. Если вы хотите применить ее к многомерному массиву, то вам необходимо использовать рекурсивную функцию.

Пример #2 Использование stripslashes() с массивом

function stripslashes_deep ( $value )
$value = is_array ( $value ) ?
array_map ( ‘stripslashes_deep’ , $value ) :
stripslashes ( $value );

// Пример
$array = array( «f\\’oo» , «b\\’ar» , array( «fo\\’o» , «b\\’ar» ));
$array = stripslashes_deep ( $array );

Результат выполнения данного примера:

Array ( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ) )

Смотрите также

  • addslashes() — Экранирует строку с помощью слешей
  • get_magic_quotes_gpc() — Получение текущего значения настройки конфигурации magic_quotes_gpc

Источник

How to Use stripslashes() Function in PHP

In PHP, we have different functions to deal with strings. One of these functions is stripslashes(), which remove backslashes from a string. This article covers stripslashes() function, its syntax, parameter, return value, and an example PHP program that explains its usage.

What is stripslashes() Function in PHP

In PHP, a backslash (\) is used to escape special characters, such as quotes or apostrophes, within a string. However, if we are getting data from an external source, such as a form submission, the data may contain extra backslashes. This is where stripslashes() function can be helpful.

The stripslashes() function removes all backslashes that have been added to a string, except for those that have been used to escape quotes or apostrophes.

Syntax

The basic syntax for using stripslashes() in PHP is as follows:

Here, $string is the string that we want to remove the backslashes from.

Parameters

This function contains one parameter:

Return Value

The stripslashes() function returns the input string with all backslashes (\) removed, except for those that are used to escape quotes or apostrophes.

Example Code

Below is the given code that uses the stripslashes() function to remove backslashes from a string, and then displays the result using the echo statement.

The string variable $str is defined with a value of Wel\come To Linux\hint. The stripslashes() function is called with the $str variable as its argument. This function removes any backslashes in the string, except for those that are used to escape quotes or apostrophes.

The result of stripslashes() is then printed to the screen using the echo statement. The output of this code would be Welcome To Linuxhint:

Using stripslashes() with Arrays in PHP

When working with form data, we may need to use stripslashes() with an array of strings. In this case, the array_map() function can be used to apply stripslashes() to each element in the array.

Now we will cover how to implement the stripslashes() function recursively for an array. Since stripslashes() is not a recursive function, a separate recursive function is defined to apply this function to each element of the input array. This allows for the removal of backslashes from all elements of the array.

Example Code

Following PHP code recursively removes backslashes from an array or string.

function stripslashes_arr ( $value )
{
$value = is_array ( $value ) ?
array_map ( ‘stripslashes_arr’ , $value ) :
stripslashes ( $value ) ;

return $value ;
}
$array = array ( «Wel \\ come» , «T \\ o» , » \\ Linuxhint» ) ;
$array = stripslashes_arr ( $array ) ;
print_r ( $array ) ;
?>

At the start of the code, we have defined a function called stripslashes_arr() that recursively removes backslashes from an array or string. It then defines an array containing values with backslashes and applies the stripslashes_arr() function to this array.

And at the end of the code, it prints the resulting array to the screen using the print_r() function.

Output

The output of this code would be an array with the values Welcome, To, and Linuxhint without any backslashes.

Conclusion

The stripslashes() function removes all backslashes that have been added to a string, except for those that have been used to escape quotes or apostrophes. This function can also be applied to both individual strings and arrays of strings. For a detailed description of stripslashes() function, its syntax, and its parameters, read the article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.

Источник

stripslashes

stripslashes() can be used if you aren’t inserting this data into a place (such as a database) that requires escaping. For example, if you’re simply outputting data straight from an HTML form.

Parameters

Return Values

Returns a string with backslashes stripped off. ( \’ becomes ‘ and so on.) Double backslashes ( \\ ) are made into a single backslash ( \ ).

Examples

Example #1 A stripslashes() example

 $str = "Is your name O\'reilly?"; // Outputs: Is your name O'reilly? echo stripslashes($str); ?>

Note:

stripslashes() is not recursive. If you want to apply this function to a multi-dimensional array, you need to use a recursive function.

Example #2 Using stripslashes() on an array

 function stripslashes_deep($value) < $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; > // Example $array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")); $array = stripslashes_deep($array); // Output print_r($array); ?>

The above example will output:

Array ( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ) )

Источник

stripslashes

stripslashes () можно использовать, если вы не вставляете эти данные в место (например, базу данных), требующее экранирования. Например, если вы просто выводите данные прямо из HTML-формы.

Parameters

Return Values

Возвращает строку с удаленными обратными косыми чертами. ( \’ становится ‘ и т. д.) Двойные обратные косые черты ( \\ ) превращаются в одиночные обратные косые черты ( \ ).

Examples

Пример # 1 stripslashes () Пример

 $str = "Is your name O\'reilly?"; // Outputs: Is your name O'reilly? echo stripslashes($str); ?>

Note:

stripslashes () не рекурсивен. Если вы хотите применить эту функцию к многомерному массиву, вам необходимо использовать рекурсивную функцию.

Пример # 2 Использование stripslashes () в массиве

 function stripslashes_deep($value) < $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; > // Example $array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")); $array = stripslashes_deep($array); // Output print_r($array); ?>

Выводится приведенный выше пример:

Array ( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ) )

Источник

Читайте также:  Java set добавить элемент
Оцените статью