By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

ereg_replace eat slash

P: n/a
Someone could tell me why preg_replace eat a slash when it do replacement?

<?
$stringToReplace = "blah blah {nome} blah blah";
$replacement = "Two slash: \\\\";
$stringToReplace =
preg_replace("/{nome}/Us",$replacement,$stringToReplace,1);
echo($stringToReplace);

#
# It print
# blah blah Two slash: \ blah blah
#
# instead of
# blah blah Two slash: \\ blah blah
#
# Why it eat a slash?
#
?>
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Xaradas wrote:
Someone could tell me why preg_replace eat a slash when it do replacement?

<?
$stringToReplace = "blah blah {nome} blah blah";
$replacement = "Two slash: \\\\";
internaly, $replacement is "Two slash: \\"
$stringToReplace =
preg_replace("/{nome}/Us",$replacement,$stringToReplace,1);
Lets put $replacement contents inside the preg_replace (as php will do)

preg_replace("/{nome}/Us","Two slash: \\",$stringToReplace,1);
echo($stringToReplace);

#
# It print
# blah blah Two slash: \ blah blah
Right! preg_replace() evaluates the replacement like an ordinary string.

Try

$replacement = "Two slash: \\\\\\\\"; /* redoubled because */
/* it will be used twice */
?>


--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
Xaradas wrote:
Someone could tell me why preg_replace eat a slash when it do replacement?

<?
$stringToReplace = "blah blah {nome} blah blah";
$replacement = "Two slash: \\\\";

internaly, $replacement is "Two slash: \\"

$stringToReplace =
preg_replace("/{nome}/Us",$replacement,$stringToReplace,1);

Lets put $replacement contents inside the preg_replace (as php will do)

preg_replace("/{nome}/Us","Two slash: \\",$stringToReplace,1);

echo($stringToReplace);

#
# It print
# blah blah Two slash: \ blah blah

Right! preg_replace() evaluates the replacement like an ordinary string.

Try

$replacement = "Two slash: \\\\\\\\"; /* redoubled because */
/* it will be used twice */

?>



I've check your solution and it's ok but why using
ereg_replace I don't need to redouble?

<?

$stringToReplace = "blah blah {nome} blah blah";
$replacement = "Two slash: \\\\";
$stringToReplace = ereg_replace("{nome}",$replacement,$stringToReplac e);
echo($stringToReplace);

# It print
# blah blah Two slash: \\ blah bla

?>
Jul 17 '05 #3

P: n/a

"Xaradas" <NO****@NOSPAM.NO> wrote in message
news:O2**********************@news4.tin.it...
Someone could tell me why preg_replace eat a slash when it do replacement?


That's because the replacement string isn't treated simple text.
preg_replace() can insert captured result into the replacement string. For
sample, preg_replace('/I hate (\w+)/', 'I love \1', 'I hate Perl!') would
yield "I love Perl!" Because the backslash is used to indicate the captured
string, there needs to be a way of escaping it, in case you really want
'\1'.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.