Jump to content

Recommended Posts

I have a table and a field has a bunch of entries with backslashes (\) from some other script.  I want to clean them all out.

the bs's are not all in the same place

example entry:

buzz\_bait    --->    buzz_bait

\[HENRY\]    --->    [HENRY]

etc...

I don't think subst() will do it because the bs's are in different position each time... can someone suggest the right function/command to use in PHP?
Link to comment
https://forums.phpfreaks.com/topic/31883-solved-easy-one/
Share on other sites

stripslashes() won't do the job here- because it doesnt remove the slashes from things like underscores or "[".
You can use this function- $string should be the string that you want to remove the slashes from, and escaped should be an array containing all of the chars that got escaped and you want to un-escape.

[code]<?php

function unescape($string, $escaped)
{
$b_slash = "\\";
foreach($escaped as $char)
$string = str_replace($b_slash.$char, $char, $string);

return $string;
}


$replace = array("[", "]", "&", "#");
$string = "\\[Hello\\] \\& \\% \\' \\#"; //I've escaped the backslashes because I have to, not for the test
echo $string;
echo "<br>";
echo unescape($string, $replace);


/*
Output:
\[Hello\] \& \% \' \#
[Hello] & \% \' #
*/

?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/31883-solved-easy-one/#findComment-147975
Share on other sites

[quote author=Orio link=topic=119953.msg491770#msg491770 date=1167153853]
stripslashes() won't do the job here- because it doesnt remove the slashes from things like underscores or "[".
[/quote]

Well I just tested this..

[code]
<?php
$string = 'this is\_a \[test\]';

echo stripslashes($string);
?>
[/code]

The output was this is_a [test].

This worked fine.. it removed all \ s from the string.. I tried in several different ways all with the same out come..

So I guess, unless I missed something, that stripslashes would work fine..
Link to comment
https://forums.phpfreaks.com/topic/31883-solved-easy-one/#findComment-148012
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.