Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all slashes regex

Tags:

regex

replace

php

I have string "h///e/ll\\o\//\". Need to remove all slashes from it back and forth slashes multiple times can someone show me regex to do this?

its for php preg_replace();

like image 517
JohnA Avatar asked Dec 22 '22 02:12

JohnA


2 Answers

Try this:

var_dump(preg_replace("@[/\\\]@", "", "h///e/ll\\o\\//\\"));
// Output: string(5) "hello"

http://codepad.org/PIjKsc9F

Or alternativly

var_dump(str_replace(array('\\', '/'), '', 'h///e/ll\\o\\//\\'));
// Output: string(5) "hello"

http://codepad.org/0d5j9Mmm

like image 142
Petah Avatar answered Dec 24 '22 01:12

Petah


You don't need a regex to remove these:

$string = str_replace(array('/', '\\'), '', $string);
like image 27
cmbuckley Avatar answered Dec 24 '22 01:12

cmbuckley