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

unexpected preg_replace behavior

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:

"x"<br>"x"

the output i actually get follows:

"xx"<br>"x
x
x
x
x
x"

so why the difference? why do i get two x's instead of one x for the
first preg_replace, and why does the preg_replace for the new line
characters not give me the same thing as the first preg_replace?
Jul 17 '05 #1
1 2224
yawnmoth wrote:
say i have the following script:

<?
$test = "aaaaa";
print '"' . preg_replace('/.*/','x',$test) . '"<br>';
Somehow preg_replace breaks "aaaaa" into "aaaaa" and "" (the empty
string). I checked with

$n = preg_match_all('/.*/', $test, $matches);
echo $n, ' matches: '; print_r($matches);

I couldn't find a modifier to stop that behaviour, but maybe you can
change your preg_replace to

preg_replace('/.+/', 'x', $test)

matching 1 or more characters instead of 0 or more.
$test = "\n\n\n\n\n";
print '"' . preg_replace('/.*/','x',$test) . '"';
The regular expression "." does not match "\n" unless you say so with
the "/s" modifier

preg_replace('/.*/s', 'x', $test)

Like this (with "*") it will match twice: for "\n\n\n\n\n" and ""
Maybe you'd like to change the "*" to a "+" here too.
?>

the output i would expect is as follows:

"x"<br>"x"

the output i actually get follows:

"xx"<br>"x
x
x
x
x
x"

so why the difference? why do i get two x's instead of one x for the
first preg_replace, and why does the preg_replace for the new line
characters not give me the same thing as the first preg_replace?


See above :)

--
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 #2

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

Similar topics

7
by: Margaret MacDonald | last post by:
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...
3
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 ...
7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
6
by: comp.lang.php | last post by:
Offensive Line: $contents = preg_replace('/(*\*+*@+)' .. str_replace('/', '\\/', preg_quote($oldversion)) . '(*)/e', '$1' . $newversion . '$2', $contents); Bluntly put, I simply don't get...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
62
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
2
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
5
by: devereaux | last post by:
I'm trying to run a script and it's throwing the following error: Parse error: syntax error, unexpected T_OBJECT_OPERATOR in openfile.php on line 41 Here is the openfile.php file and I have...
8
by: illuzion | last post by:
ok I keep getting this error: Parse error: syntax error, unexpected T_VARIABLE in /home/illuzion/public_html/BAMF/contactus.php on line 38 and this error is possibly on other lines could...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.