473,394 Members | 1,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ereg_replace eat slash

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
3 2511
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roland Sippel | last post by:
Is there anything change in "ereg_replace()" PHP > 4.3.2. Scripts works in PHP 4.3.1 but NOT in PHP 4.3.2: $text = "<!-- test -->"; $text = ereg_replace("]+"," ",strip_tags($text)); echo $text...
2
by: Chris | last post by:
Hello, I use a javascript ticker in my header which contains the news in a array like that tickercontents='Message1' tickercontents='Message2' tickercontents='Message3' now i want to write an...
4
by: knocte | last post by:
Hello. I am testing this curious code and I don't understand yet why it doesn't work with the 2nd case: Case 1: $number = "2,4"; $number = ereg_replace(",",".",$number); echo $number; //...
0
by: pieterprovoost | last post by:
Hi, I would like to use the ereg_replace() function to replace wiki-like syntax, for example: ] becomes <a href="index.php?page=somepage">somepage</a> **hello** becomes <i>hello</i>
14
by: fakefaker123 | last post by:
Hi, Does anyone know what exactly this does: ereg_replace("", "", $value) would this be responsible for a negative number losing it's minus sign? e.g, if @value is minus 2300.00 (-2300.00), then...
6
by: Bob Bedford | last post by:
Hello there, I need help: I've a string that is used to write down a phone number that comes from a form and would like to keep it as clean as possible. For avoiding people to create twice an...
1
by: tmcp | last post by:
Hello I'm a bit new to regular expressions and I'm having a problem I'm trying to strip any code which is inside a <p> tag for instance from <p style="margin: 0in 0in 0pt;" class="MsoNormal"> to...
2
by: encepif | last post by:
I think this is the right command., could someone please show me how to replace instances of a quote like this " with its html version - &quot;. I am mixed up with the escaping, etc. Thank you :-) ...
5
by: Hugh Oxford | last post by:
I have a string that has been saved in a database from a textarea form field. e.g. $text = "Dear %name The date is %date yours,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.