-1

I would like to remove the trailing backslash from a string without using stripslashes() or str_replace(). Ideally I would be able to use rtrim(), but its something about the backslashes that has PHP freaking out.

$string = "This is my string\";

//iv'e tried with no success
$clean_string = rtrim($string, "\\");
$clean_string = rtrim($string, "\\\\");

Ideally the string would just read "This is my string" without the backslash at the end. I'm not entirely sure how to escape it properly, any help is much appreciated.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Niko Johnson
  • 31
  • 1
  • 5
  • 1
    You might have meant `$string = "This is my string\\";`. To define a ``\`` in a double or single quote PHP string literal, you need to double it. – Wiktor Stribiżew Oct 17 '16 at 17:48
  • Thanks for your reply. The string is not being created via hardcode like that the example above.. It takes in a CSV, then splits up the appropriate rows/columns.. The string here represents the value that is in any of the "cells" of the CSV – Niko Johnson Oct 17 '16 at 17:55
  • Both of your methods work fine. The problem is the backslash in `$string`.[Check it](http://sandbox.onlinephpfunctions.com/code/4f269e1b87d35d3001348e21fdaf0adfd892c7e2) – hlscalon Oct 17 '16 at 17:55
  • http://php.net/manual/en/function.stripslashes.php ??? – AbraCadaver Oct 17 '16 at 18:11

3 Answers3

1

Try this:

 if(substr($string, -1) == "\"){ 
   echo substr($string, 0, -1);
 }

if condition checks whether the last character has slash or not.

Kinshuk Lahiri
  • 1,468
  • 10
  • 23
  • Thanks for the speedy reply... The problem is, this is for an import script, and not every string will have the trailing backslash... I do not wish to remove the last charter everytime. – Niko Johnson Oct 17 '16 at 17:52
  • @NikoJohnson Updated the answer – Kinshuk Lahiri Oct 17 '16 at 17:55
  • Thanks for the fix.. It appears to also not be escaped the right way... even if i use – Niko Johnson Oct 17 '16 at 17:56
  • @NikoJohnson Are you getting single backslash or multiple or unaware of it. – Kinshuk Lahiri Oct 17 '16 at 17:58
  • Oops, accidentally hit enter too quickly. Anyway, i really appreciate your help, but the backslash is not escaped properly... That's where I'm confused, I dont know how to get php to recognize a backslash.. :( – Niko Johnson Oct 17 '16 at 17:58
  • As far as im aware, its just 1 backslash.. I'm at a loss, I know everything mentioned here should work but its just not cleaning the string as expected.. Perhaps I have another bug that is causing this one.. Day in the life of a dev right? Anyway, i appreciate you entertaining this seemingly Noob AF question with a helpful answer and good hospitality. – Niko Johnson Oct 17 '16 at 18:12
  • can you please provide the output for this `print_r($string)`. – Kinshuk Lahiri Oct 17 '16 at 18:14
1

This worked for me:

    if(substr($string, -1) == '\\')
    { 
        $string = substr($string, 0, -1);
    }
Wonko the Sane
  • 754
  • 3
  • 14
  • 31
0

You may try preg_replace:

$string = 'This is my string\\';
$clean_string = preg_replace('/(.+)(\\\\)$/', '${1}', $string);

The trailing slash will get removed if pattern is matched. You will get the same string otherwise.

Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59