473,378 Members | 1,146 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,378 software developers and data experts.

back slashes and preg_replace's e modifier

Can anyone help me understand the following scripts output?:

<?
echo preg_replace('/.*/e',"sprintf('\\\\\')",'');
?>

The output is \ and this doesn't make a lot of sense to me.

Since \\'s in strings are interpreted as single \'s, the above sprintf
parameter is equal to \\\. The following scripts output confirms this:

<?
echo "sprintf('\\\\\')";
?>

Attempting to run the output of the second script (as done in the next
script), however, results in a syntax error:

<?
echo sprintf('\\\');
?>

The fact that a syntax error would result isn't particurarly surprising
since the last ' is being escaped. What is surprising, however, is
that the last script yields a syntax error whereas the first script
doesn't. Any ideas as to why?

Nov 5 '05 #1
2 3550
yawnmoth said the following on 05/11/2005 22:29:
Can anyone help me understand the following scripts output?:

<?
echo preg_replace('/.*/e',"sprintf('\\\\\')",'');
?>


I would imagine you have the 2nd and 3rd arguments the wrong way round.


--
Oli
Nov 5 '05 #2

Oli Filth wrote:
yawnmoth said the following on 05/11/2005 22:29:
Can anyone help me understand the following scripts output?:

<?
echo preg_replace('/.*/e',"sprintf('\\\\\')",'');
?>


I would imagine you have the 2nd and 3rd arguments the wrong way round.


The order of the arguments is actually intentional. I'm trying to
replace the empty string (the third argument) with that which the
second argument would output when ran (as per the e modifier).

The following demonstrates that replacement should be taking place:

<?
echo preg_replace('/.*/','replacement','');
?>

Nov 5 '05 #3

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

Similar topics

1
by: Richard B. Christ | last post by:
I wrote the following code and it does NOT seem to work. $search = array('/<popup*>/ie'); $replace = array('make_popup(split_tag(\\0))'); preg_replace($search, $replace, $someText); If I try...
8
by: Jochen Daum | last post by:
Hi, I'd like to use something like preg_replace("...","$this->somefunction('\\1','\\2');", $content); It says "Cannot find function somefunction()" I tried some escaping, but didn't...
1
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...
2
by: brendan | last post by:
trying to clear out all text between the outermost <---Message Start---> and <---Message End---> tags using snippet
5
by: Sorby | last post by:
Hi, My website needs to allow users to specify where a thumbnail image will appear in the text ($articleText) they submit. The thumbnail filenames are held in a single-dimensional array called...
1
by: correo | last post by:
Hi all! This: $string = preg_replace('//i', '_', $string); replaces an accented letter with two underscores instead of one, when the submitting page is in UTF8 ($string comes from a GET...
1
by: rybasso | last post by:
Hello, Does anyone has an idea why preg_replace() cause 'This page contains no data' in FF and 'This page could not be found' in IE. I spend a lot of time searching any ideas on newsgroups but...
3
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 =...
0
by: Sebarry | last post by:
Hi, I'm a PHP newbie and I don't understand what it means about the e modifier makes preg_replace() treat the replacement parameter as PHP code after the appropriate references substitution is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.