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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

Terry WoodsIT GuruCommented:
$string = preg_replace("@\\$@","",$string);
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

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

XK8ERAuthor Commented:
this doesnt work..

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


how can I test the other above samples?
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

XK8ERAuthor Commented:
it shows a blank page while testing
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

Terry WoodsIT GuruCommented:
Sorry, correction:


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

Open in new window

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

Open in new window

XK8ERAuthor Commented:
ok the output of that shows as

A Christmas Carol \

and it should be

A Christmas Carol
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

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

Open in new window

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

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?
Terry WoodsIT GuruCommented:
Yes, that's right.

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..
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.