use preg replace to remove last digit

hello there,
if there is a back slash in the last digit of a string like this

A Christmas Carol \

how can I remove it and just end up like this

A Christmas Carol
LVL 1
XK8ERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theodorejsalvoCommented:
Use this.
$str = preg_replace('/\\$/','',$str);

Open in new window

0
Terry WoodsIT GuruCommented:
$string = preg_replace("@\\$@","",$string);
0
delizekaCommented:
I think you can use rtrim function to remove last backslash character at the end of string.

Example :


$text = 'A Christmas Carol \';
$new_text = rtrim($text, '\');

//new_text now does not contain a back slash at the end of it

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

XK8ERAuthor Commented:
this doesnt work..

$text = 'A Christmas Carol \';
$new_text = rtrim($text, '\');
echo $new_text;


how can I test the other above samples?
0
theodorejsalvoCommented:
This would be a test.
$text = 'A Christmas Carol \';
$new_text = preg_replace('/\\$/','',$text);
$new_text = $new_text[0];
echo $new_text;

Open in new window

0
XK8ERAuthor Commented:
it shows a blank page while testing
0
Terry WoodsIT GuruCommented:
Try this:
$text = 'A Christmas Carol \\';
$new_text = preg_replace('/\\$/','',$text);
$new_text = $new_text[0];
echo $new_text;

Open in new window

0
Terry WoodsIT GuruCommented:
Sorry, correction:


$text = 'A Christmas Carol \';
$new_text = preg_replace('/\\$/','',$text);
echo $new_text;

Open in new window

0
XK8ERAuthor Commented:
both blank page still
0
Terry WoodsIT GuruCommented:
Oops, this time then!
$text = 'A Christmas Carol \\';
$new_text = preg_replace('/\\$/','',$text);
echo $new_text;

Open in new window

0
XK8ERAuthor Commented:
ok the output of that shows as

A Christmas Carol \

and it should be

A Christmas Carol
0
Terry WoodsIT GuruCommented:
Ok, solved it - needs 4 backslashes!
$text = 'A Christmas Carol \\';
print "Before:<pre>$text</pre>";
$text = preg_replace("/\\\\$/",'',$text);
print "After:<pre>$text</pre>";

Open in new window

0
Terry WoodsIT GuruCommented:
Note this works too:
$text = rtrim($text,'\\');

Open in new window

0
theodorejsalvoCommented:
rtrim will work if you also include the CR & LF
$text = "A Christmas Carol \\";
var_dump($text);
$new_text = rtrim($text, " \\\r\n");
var_dump($new_text);

Open in new window

0
XK8ERAuthor Commented:
alright so for testing it just needs 4 backslashes but for the normal app it just needs 2 backslashes since its coming from mysql database.. correct?
0
Terry WoodsIT GuruCommented:
Yes, that's right.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XK8ERAuthor Commented:
got it...thanks a lot TerryAtOpus..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.