473,811 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3572
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
3715
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 the following code, then the replacing seems to work (I just try to get a dummy back reference \\1) $search = array('/<p(op)up*>/ie');
8
3647
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 work.
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
1969
by: brendan | last post by:
trying to clear out all text between the outermost <---Message Start---> and <---Message End---> tags using snippet
5
2521
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 $photos e.g. the users are to type to specify they want thumbnail four ( from array position 4 ) I was hoping the following expression would work, but it doesn't. The
1
489
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 form). It works well when the submitting page is encoded in ISO-8859-1 or with other characters such as an ampersand or whatever. Any ideas why? TIA
1
1783
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 found nothing corresponding. Here is the code:
3
2203
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
1324
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 done. I have a file with HTML tags such as follows. <DIV class="bottomPostionDiv"> <H1>BENEFITS </H1></DIV> I have a regular expression to identify the uppercase tags, which I want to convert to lowercase.
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
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
10135
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
9205
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...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
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.