473,746 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_replace: stripping backslashes

I've been going mad trying to figure out how to do this--it should be
easy!

Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

'_sometext_' (i.e., the same thing with the literal backslashes
stripped)

Unless I'm misunderstandin g something (I don't know Perl at all), this
should work:

preg_replace( '/\\\\\\\\_(.*?)\ \\\\\\\_/i', '_$1_', $thepostvar )

but it doesn't, and I don't know why. The filter apparently leaves
the string unchanged, since it comes across in the POST array with the
backslash doubled, and it comes out of the filter with the backslash
still doubled. It doesn't seem to matter how many backslashes I use
in the filter--I've tried between 4 and 10--the result is the same.

Any insights?

thanks in advance,
Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #1
7 4996
On Tue, 20 Jul 2004 15:14:55 +0000, Margaret MacDonald wrote:
I've been going mad trying to figure out how to do this--it should be
easy!

Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

'_sometext_' (i.e., the same thing with the literal backslashes
stripped)

Unless I'm misunderstandin g something (I don't know Perl at all), this
should work:

preg_replace( '/\\\\\\\\_(.*?)\ \\\\\\\_/i', '_$1_', $thepostvar )

but it doesn't, and I don't know why. The filter apparently leaves
the string unchanged, since it comes across in the POST array with the
backslash doubled, and it comes out of the filter with the backslash
still doubled. It doesn't seem to matter how many backslashes I use
in the filter--I've tried between 4 and 10--the result is the same.

Any insights?

thanks in advance,
Margaret

str_replace() doesn't work?
$thepostvar = str_replace("\\ ", '', $thepostvar);

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
Ian.H wrote:
On Tue, 20 Jul 2004 15:14:55 +0000, Margaret MacDonald wrote:
I've been going mad trying to figure out how to do this--it should be
easy!

Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

'_sometext_' (i.e., the same thing with the literal backslashes
stripped)

Unless I'm misunderstandin g something (I don't know Perl at all), this
should work:

preg_replace( '/\\\\\\\\_(.*?)\ \\\\\\\_/i', '_$1_', $thepostvar )

but it doesn't, and I don't know why. The filter apparently leaves
the string unchanged, since it comes across in the POST array with the
backslash doubled, and it comes out of the filter with the backslash
still doubled. It doesn't seem to matter how many backslashes I use
in the filter--I've tried between 4 and 10--the result is the same.

Any insights?

thanks in advance,
Margaret

str_replace( ) doesn't work?
$thepostvar = str_replace("\\ ", '', $thepostvar);


Thanks for the quick response, Ian!

I stripped out some unessentials :-) This filter is part of a larger
filtering process, though it fails when run alone too. I could jump
out of that larger process and call str_replace() to handle this as a
special case, but there's no particular reason I can think of why I
should have to kludge it in that way (and I'm sure it would nag me to
death if I did it :-D).

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #3
Margaret MacDonald wrote:
I've been going mad trying to figure out how to do this--it should be
easy!

Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

'_sometext_' (i.e., the same thing with the literal backslashes
stripped)

Unless I'm misunderstandin g something (I don't know Perl at all), this
should work:

preg_replace( '/\\\\\\\\_(.*?)\ \\\\\\\_/i', '_$1_', $thepostvar )

but it doesn't, and I don't know why. The filter apparently leaves
the string unchanged, since it comes across in the POST array with the
backslash doubled, and it comes out of the filter with the backslash
still doubled. It doesn't seem to matter how many backslashes I use
in the filter--I've tried between 4 and 10--the result is the same.

Any insights?


First thing that hapens here is PHP dealing with the backslashes.
Your four pairs of backslashes are sent to the preg_replace as four
single backslashes.

Now it is preg_replace()' s turn to deal with them.
It converts 4 backslashes (two pairs) to two single backslashes and
can't find them in the value of $thepostvar.

If you remove one backslash from your regular_express ion, PHP will
convert three pairs of backslashes to three single backslashes and the
remaining "\_" to "\_"; so preg_replace() will see the same thing as
above.
If you remove two backslashes from your regular expression, PHP will
convert two pairs to two single backslashes; preg_replace() will replace
one pair to one single backslash;
Try this:
<?php
$input = 'This is \_sometext\_ embedded.';

$output1 = preg_replace('@ \_(.*)\_@X', '_$1_', $input);
$output2 = preg_replace('@ \\_(.*)\\_@X', '_$1_', $input);
$output3 = preg_replace('@ \\\_(.*)\\\_@X' , '_$1_', $input);
$output4 = preg_replace('@ \\\\_(.*)\\\\_@ X', '_$1_', $input);
$output5 = preg_replace('@ \\\\\_(.*)\\\\\ _@X', '_$1_', $input);
$output6 = preg_replace('@ \\\\\\_(.*)\\\\ \\_@X', '_$1_', $input);
$output7 = preg_replace('@ \\\\\\\_(.*)\\\ \\\\_@X', '_$1_', $input);
$output8 = preg_replace('@ \\\\\\\\_(.*)\\ \\\\\\_@X', '_$1_', $input);

echo '1: ', $output1, "\n";
echo '2: ', $output2, "\n";
echo '3: ', $output3, "\n";
echo '4: ', $output4, "\n";
echo '5: ', $output5, "\n";
echo '6: ', $output6, "\n";
echo '7: ', $output7, "\n";
echo '8: ', $output8, "\n";
?>

The output is:

1: This is \_sometext\_ embedded.
2: This is \_sometext\_ embedded.
3: This is _sometext_ embedded.
4: This is _sometext_ embedded.
5: This is _sometext_ embedded.
6: This is _sometext_ embedded.
7: This is \_sometext\_ embedded.
8: This is \_sometext\_ embedded.


--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #4
"Margaret MacDonald" wrote:
Ian.H wrote:
On Tue, 20 Jul 2004 15:14:55 +0000, Margaret MacDonald wrote:
I’ve been going mad trying to figure out how to do this--it should be easy!

Allow the user to enter ’\_sometext\_’, i.e., literal backslash, underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

’_sometext_’ (i.e., the same thing with the literal backslashes stripped)

Unless I’m misunderstandin g something (I don’t know Perl at all), this should work:

preg_replace( ’/\\\\_(.*?)\\\\_/i’, ’_ _’, $thepostvar )

but it doesn’t, and I don’t know why. The filter apparently leaves the string unchanged, since it comes across in the POST array
with the backslash doubled, and it comes out of the filter with the backslash still doubled. It doesn’t seem to matter how many backslashes I use in the filter--I’ve tried between 4 and 10--the result is the same.
Any insights?

thanks in advance,
Margaret

str_replace( ) doesn’t work?
$thepostvar = str_replace("\" , ’’, $thepostvar);


Thanks for the quick response, Ian!

I stripped out some unessentials This filter is part of a larger
filtering process, though it fails when run alone too. I could

jump out of that larger process and call str_replace() to handle this as a special case, but there’s no particular reason I can think of
why I
should have to kludge it in that way (and I’m sure it would nag
me to
death if I did it ).

Margaret


Margaret, you don’t need to "jump out" of your regular process.
Simply write a new function called str_replace_new and call that
instead. This function checks for "\" and replaces it on the fly
before checking.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-preg_rep...ict131345.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=438214
Jul 17 '05 #5
Pedro Graca wrote:
Margaret MacDonald wrote:
I've been going mad trying to figure out how to do this--it should be
easy!

Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back

'_sometext_' (i.e., the same thing with the literal backslashes
stripped)

Unless I'm misunderstandin g something (I don't know Perl at all), this
should work:

preg_replace( '/\\\\\\\\_(.*?)\ \\\\\\\_/i', '_$1_', $thepostvar )

but it doesn't, and I don't know why. The filter apparently leaves
the string unchanged, since it comes across in the POST array with the
backslash doubled, and it comes out of the filter with the backslash
still doubled. It doesn't seem to matter how many backslashes I use
in the filter--I've tried between 4 and 10--the result is the same.

Any insights?


First thing that hapens here is PHP dealing with the backslashes.
Your four pairs of backslashes are sent to the preg_replace as four
single backslashes.

Now it is preg_replace()' s turn to deal with them.
It converts 4 backslashes (two pairs) to two single backslashes and
can't find them in the value of $thepostvar.

If you remove one backslash from your regular_express ion, PHP will
convert three pairs of backslashes to three single backslashes and the
remaining "\_" to "\_"; so preg_replace() will see the same thing as
above.
If you remove two backslashes from your regular expression, PHP will
convert two pairs to two single backslashes; preg_replace() will replace
one pair to one single backslash;
Try this:
<?php
$input = 'This is \_sometext\_ embedded.';

$output1 = preg_replace('@ \_(.*)\_@X', '_$1_', $input);
$output2 = preg_replace('@ \\_(.*)\\_@X', '_$1_', $input);
$output3 = preg_replace('@ \\\_(.*)\\\_@X' , '_$1_', $input);
$output4 = preg_replace('@ \\\\_(.*)\\\\_@ X', '_$1_', $input);
$output5 = preg_replace('@ \\\\\_(.*)\\\\\ _@X', '_$1_', $input);
$output6 = preg_replace('@ \\\\\\_(.*)\\\\ \\_@X', '_$1_', $input);
$output7 = preg_replace('@ \\\\\\\_(.*)\\\ \\\\_@X', '_$1_', $input);
$output8 = preg_replace('@ \\\\\\\\_(.*)\\ \\\\\\_@X', '_$1_', $input);

echo '1: ', $output1, "\n";
echo '2: ', $output2, "\n";
echo '3: ', $output3, "\n";
echo '4: ', $output4, "\n";
echo '5: ', $output5, "\n";
echo '6: ', $output6, "\n";
echo '7: ', $output7, "\n";
echo '8: ', $output8, "\n";
?>

The output is:

1: This is \_sometext\_ embedded.
2: This is \_sometext\_ embedded.
3: This is _sometext_ embedded.
4: This is _sometext_ embedded.
5: This is _sometext_ embedded.
6: This is _sometext_ embedded.
7: This is \_sometext\_ embedded.
8: This is \_sometext\_ embedded.


Thanks for your quick response, Pedro!

Interestingly, it turns out that PHP loses track of the fact that the
string started out with two single backslash literals. So running
your test (and very neat it is, too...I can't imagine why I didn't
think of doing that; I must have been having an 'Einstein moment' :-)
works fine as long as I set the var locally before running the filter.
Both \_test_\ and \\_test\\_ evaluate as strings of length 8 when
set locally...but as strings of length 12 and 14 respectively after
being passed via the POST array! (which feels like a bug, to me).

So to properly filter on '\_test\_' after passing it through the POST
array, I've to use 8(!) backslashes, not 4.

I still don't know why it wasn't working to begin with, since I did
try it with 8. [mutter mutter]

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #6
er, that should have been '10 and 12 respectively', of course. It's
amazing what one can type without noticing, when falling-down tired.
*sigh*

I wrote:
set locally...but as strings of length 12 and 14 respectively after


--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #7
Margaret MacDonald wrote:
that should have been '10 and 12 respectively', of course.


magic_quotes on?

I don't like them!

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4321
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
1
2243
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the output i would expect is as follows:
2
7137
by: Fotozine | last post by:
well, I have this peace of code which isn't woking as I would like it to work. $tekst = preg_replace('/\\\"/', '&quot;', $tekst); In my forms, I would like to change " to &quot; but this line is actualy "stripnig" text that it looks (or "cames to mysql db) like: Lorem ipsum \"
3
5326
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
6
24564
by: Mikheil | last post by:
Hello! I need to translate file destination name with one backslashes "c:\program files\directory\file.txt" to string containing double backslashes "c:\\program files\\directory\\file.txt" If there is a nativi function on C++, or any algolithms I'd be glad to know about them. Thank you
5
498
by: julianmlp | last post by:
I'm fairly new to regex code, and I'm trying to find a way to replace any text after "///" (three slashes) and before a "\n" character. I've tried with: $filen = preg_replace("///./\n$", "", $filen); The pattern would mean: "string that has three "/" followed by any characters and which ends with \n"
8
2372
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets or quotation marks around them as well as lines that start with a hyphen. Lastly the lines with ** need the brackets before the **, instead of at the end of the line. Here is what I have so far - it is close, but I keep ending up with
3
13491
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
OpenFileDialog gives me the following, which I place in tbDevPath.Text: x:\\myVob\\mySolution\\mySolution.sln I really need this path to be single-backslashes, not double-backslashes, so I tried the following: string updatedDevPath = Regex.Replace(tbDevPath.Text, @"^\\\\$", "\\") This still leaves me with double-backslashes. Any suggestions?
3
2201
by: amygdala | last post by:
Hi there, I'm trying to replace single quoted attributes in HTML tags with double quotes. What I've come up with is this: function replaceSingleQuotesInsideTags( $input ) { $output = preg_replace( '/<*>/e', "str_replace( '\'', '\"', '$0' )",
0
9508
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9218
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8229
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.