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

strange behaviour: preg_replace with /e modifier and str_replace

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' )",
$input
);
return $output;
}

So let's say I have this string of HTML code:

On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE='font-style: italic'>stuff</SPAN>

Result:
On <A href=\"/home/\" target=\"_self\">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

As you can see, when I run it through my function
replaceSingleQuotesInsideTags, not only does it replace the single quotes
with double quotes, but it also adds backslashes before double quotes that
were already present. This is unexpected behaviour to me.

Expected result:
On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

Note the missing backslashes in the anchor tag attributes.

Does anybody have an idea of what is going on here?

Working with PHP 5.1.4, magic_quotes (all) off.

Thank you in advance.
May 28 '07 #1
3 2181
At Mon, 28 May 2007 08:18:14 +0200, amygdala let h(is|er) monkeys type:
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' )",
$input
);
return $output;
}

So let's say I have this string of HTML code:

On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE='font-style: italic'>stuff</SPAN>

Result:
On <A href=\"/home/\" target=\"_self\">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>
Quoted verbatim from the preg_replace manual section:

When using the e modifier, this function escapes some characters
(namely ', ", \ and NULL) in the strings that replace the
backreferences. This is done to ensure that no syntax errors arise
from backreference usage with either single or double quotes (e.g.
'strlen(\'$1\')+strlen("$2")'). Make sure you are aware of PHP's
string syntax to know exactly how the interpreted string will look
like.

Seems a stripslashes to finish up is what you need.
Regards,

--
Schraalhans Keukenmeester - sc*********@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]
"strcmp('apples','oranges') is -1"

May 28 '07 #2
You could try something like:

"strtr('$0', array('\\\"' ='\"', '\'' ='\"'))"

instead. That is known behavior of the /e modifier...

On May 28, 2:18 am, "amygdala" <nore...@noreply.comwrote:
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' )",
$input
);
return $output;

}

So let's say I have this string of HTML code:

On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE='font-style: italic'>stuff</SPAN>

Result:
On <A href=\"/home/\" target=\"_self\">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

As you can see, when I run it through my function
replaceSingleQuotesInsideTags, not only does it replace the single quotes
with double quotes, but it also adds backslashes before double quotes that
were already present. This is unexpected behaviour to me.

Expected result:
On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

Note the missing backslashes in the anchor tag attributes.

Does anybody have an idea of what is going on here?

Working with PHP 5.1.4, magic_quotes (all) off.

Thank you in advance.

May 28 '07 #3

"amygdala" <no*****@noreply.comschreef in bericht
news:46***********************@news.kpnplanet.nl.. .
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' )",
$input
);
return $output;
}

So let's say I have this string of HTML code:

On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE='font-style: italic'>stuff</SPAN>

Result:
On <A href=\"/home/\" target=\"_self\">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

As you can see, when I run it through my function
replaceSingleQuotesInsideTags, not only does it replace the single quotes
with double quotes, but it also adds backslashes before double quotes that
were already present. This is unexpected behaviour to me.

Expected result:
On <A href="/home/" target="_self">this</Apage you will find <SPAN
STYLE="font-style: italic">stuff</SPAN>

Note the missing backslashes in the anchor tag attributes.

Does anybody have an idea of what is going on here?

Working with PHP 5.1.4, magic_quotes (all) off.

Thank you in advance.
Aaah yes, I should have rtfm. My bad. Thanks people.
May 28 '07 #4

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

Similar topics

1
by: fartsniff | last post by:
i found this code out in the ng, and its seems long and clunky, i am still experimenting with preg_match and _replace, but the syntax is a bit confusing. it seems i always misplace or mistype...
3
by: JDJones | last post by:
I'm having some trouble getting this replacement to work efficiently. I have the following code: $thing = preg_replace("(\r\n|\n|\r|\t)", "", $thing1); What I want to do now is to get it to...
3
by: Justin Koivisto | last post by:
I am replacing a string in a text block that has a literal $ in it, and preg_replace is seeing it as a backreference. Here is what I am using: foreach($price_lists as $list)...
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...
6
by: JDJones | last post by:
Just want to verify. I have a form and I want to parse any semi-colons out of the submitted info and replace with commas. Would this be the correct way to do it? $question1 = preg_replace(";",...
3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
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...
2
by: yawnmoth | last post by:
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...
10
by: deko | last post by:
I need to capture referrers to a website and populate a page with links to the referring pages. How do I use preg_replace to search for and replace all ampersands ('&') with the corresponding...
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: 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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.