strange behaviour: preg_replace with /e modifier and str_replace

This is a discussion on strange behaviour: preg_replace with /e modifier and str_replace within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I'm trying to replace single quoted attributes in HTML tags with double quotes. What I've come ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2007
amygdala
 
Posts: n/a
Default 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</A> page you will find <SPAN
STYLE='font-style: italic'>stuff</SPAN>

Result:
On <A href=\"/home/\" target=\"_self\">this</A> page 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</A> page 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.


Reply With Quote
  #2 (permalink)  
Old 05-28-2007
Schraalhans Keukenmeester
 
Posts: n/a
Default Re: strange behaviour: preg_replace with /e modifier and str_replace

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</A> page you will find <SPAN
> STYLE='font-style: italic'>stuff</SPAN>
>
> Result:
> On <A href=\"/home/\" target=\"_self\">this</A> page 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 - schraalhans@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]
"strcmp('apples','oranges') is -1"

Reply With Quote
  #3 (permalink)  
Old 05-28-2007
petersprc
 
Posts: n/a
Default Re: strange behaviour: preg_replace with /e modifier and str_replace

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.com> wrote:
> 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</A> page you will find <SPAN
> STYLE='font-style: italic'>stuff</SPAN>
>
> Result:
> On <A href=\"/home/\" target=\"_self\">this</A> page 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</A> page 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.



Reply With Quote
  #4 (permalink)  
Old 05-28-2007
amygdala
 
Posts: n/a
Default Re: strange behaviour: preg_replace with /e modifier and str_replace


"amygdala" <noreply@noreply.com> schreef in bericht
news:465a7469$0$16940$9a622dc7@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</A> page you will find <SPAN
> STYLE='font-style: italic'>stuff</SPAN>
>
> Result:
> On <A href=\"/home/\" target=\"_self\">this</A> page 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</A> page 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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:50 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0