Kanika
Kanika

Reputation: 10708

Remove forward slashes from a string

I am trying to do some PHP programming concepts and I am not aware of some in-build functions. So my doubt is:

In PHP, how to remove slashes from strings? Is there any function available in PHP for this?

e.g.

$string = "people are using Iphone/'s instead of Android phone/'s";

Upvotes: 15

Views: 60658

Answers (8)

Farid
Farid

Reputation: 1

I tried this method to remove single forward slashes.

I used str_replace to strip the slashes out. It still did not work for me, I had to go and change all the double quotes in the database to single quotes, update the table, then change it back to double quotes for it to work. Weird.

str_replace('\\', '', $content)

Upvotes: 0

antelove
antelove

Reputation: 3348

Use varian preg

$string="people are using Iphone/'s instead of Android phone/'s";

echo $string = preg_replace('/\//', '', $string);

body, html, iframe { 
  width: 100% ;
  height: 100% ;
  overflow: hidden ;
}
<iframe src="https://ideone.com/uIBINP" ></iframe>

Upvotes: 0

zzapper
zzapper

Reputation: 5043

backslashes need escaping

$newstr = "<h1>Hello \ fred</h1>";

echo str_replace('\\','',$newstr);

Upvotes: 5

jiten
jiten

Reputation: 5264

you can use function like

 $string = preg_replace ("~/~", "", $string);

Upvotes: 2

Adam
Adam

Reputation: 1975

Heres what I use

function removeSlashes($string = '')
{
    return stripslashes(str_replace('/', '', $string));
}

Test

echo $this->removeSlashes('asdasd/asd/asd//as/d/asdzfdzdzd\\hd\h\d\h\dw');

Output

asdasdasdasdasdasdzfdzdzdhdhdhdw

Upvotes: 2

DaveRandom
DaveRandom

Reputation: 88647

You can do a number of things here, but the two approaches I would choose from are:

Use str_replace():

$string = "people are using Iphone/'s instead of Android phone/'s";
$result = str_replace('/','',$string);
echo $result;
// Output: people are using Iphone's instead of Android phone's

If the slashes are backward slashes (as they probably are), you can use stripslashes():

$string = "people are using Iphone\\'s instead of Android phone\\'s";
$result = stripslashes($string);
echo $result;
// Output: people are using Iphone's instead of Android phone's

Upvotes: 42

manan
manan

Reputation: 234

You can use stripslashes() function.

<?php
$str = "Is your name O\'reilly?";

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

Upvotes: -7

abhinav
abhinav

Reputation: 3217

If it is a quoted string. Use stripslashes

Upvotes: 2

Related Questions