How to remove hidden slash using php?

I use the Google Drive API, and the refresh_tokenone I get has a hidden slash. Although this must be a valid JSON, the API will not accept it when called refreshToken(). I am trying to remove the backslash with preg_replace:

$access_token = "1\/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8";
$access_token = preg_replace('/\\\//', '/', $access_token);

I would like the returned string to be:

"1/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8";

I tried various expressions, but it either does not remove the backslash or returns an empty string. Note that I do not want to remove all backslashes, only those that escape the slash.

+3
source share
7 answers

Avoid regular expressions and just use str_replace:

$access_token = "1\/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8";
$access_token = str_replace( '\/', '/', $access_token );
//=> 1/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8
+9
source

, , : stripslashes

, , , str_replace .

, :

$access_token = stripslashes($access_token);
+8

. ~ /.

$access_token = "1\/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8";
$access_token = preg_replace('~\\\/~', '/', $access_token);

print $access_token;

:

1/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8
+1
<?php
$access_token = "1\/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8";
echo str_replace("\\","",$access_token);
?>

:

1/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8

+1

:

$access_token = preg_replace('|\\\\|', '', $access_token);
0

, 1\\/Mg\otw\\\/Btow?

, \/ replace / .

non-\ char: '~(?<!\\\)((?:\\\\\\\)*)\\\(/)~'

- (?<!\\)((?:\\\\)*)\\(/)
- $1$2

0

( ):

$pattern = "/(?!\/)[\w\s]+/"
preg_match($pattern, $this->name,$matches)
$this->name = $matches[0]

: WOLVERINE WORLD WIDE INC/DE/

: WOLVERINE WORLD WIDE INC DE

0
source

All Articles