2016-08-19 14 views
0

私は、関数encode_entitiesによって解析される文字列を持っています。二重引用符と一重引用符を保持する必要があるので、これらの文字をフラグに置き換えてからの後にフラグを二重引用符で囲んで置き換えます。符号化されていないエンティティを符号化アルゴリズムから保護する

私の戦略は次のとおりです。

// this is a value in the database 
my $comment = "<br/>Test<br/>[url=#|onclick="printcoupon('3569')"]test comment[/url]"; 
$comment =~ s/"/[dq]/g; 
$comment =~ s/'/[sq]/g; 
$comment = encode_entities($comment); 
if(index($comment,"onclick") != -1){ 
my $start = index($comment,"onclick="); 
my $length = index($comment,"\"]"); 
my $newStr = substr($comment,$start,$length-$start+1); 
$comment =~ s!\[url=(.+?)\](.+?)\[\/url\]!<a href="#" style="text-decoration:none;color:#336699" $newStr>$2</a>!g; 
} else { 
$comment =~ s!\[url=(.+?)\](.+?)\[\/url\]!<a href="$1" style="text-decoration:none;color:#336699">$2</a>!g; 
} 
$comment =~ s/\[dq\]/"/g; 
$comment =~ s/\[sq\]/'/g; 

このコードは機能しませんが、私の戦略です。これらの代替の呼び出しがなければ、最終的な結果は次のとおりです。

<a href="#" style="text-decoration:none;color:#336699" onclick=&quot;printcoupon(&#39;3569&#39;)>test comment</a> 

とき、それはまた

<a href="#" style="text-decoration:none;color:#336699" onclick="printcoupon('3569')">test comment</a> 

する必要があり、あなたはprintcoupon機能テキストコールの終了二重引用符が存在しないことがわかります

私はまだ 非常に新しいperlのユーザー

だとして、この時点を過ぎて助けを必要と

$comment =~ s/&quot;/"/g; 
$comment =~ s/&#39;/'/g; 

UPDATEが動作するようですが、それはまだ終わり、二重引用符

答えて

0

を持っていません。変換テーブル(char2entityハッシュ)の値をリセットすることが可能です。

use HTML::Entities; 
$HTML::Entities::char2entity{'\''} = '\''; 
$HTML::Entities::char2entity{'"'} = '"'; 


my $comment = qq(<br/>Test<br/>[url=#|onclick="printcoupon('3569')"]test comment[/url]); 
$comment = HTML::Entities::encode_entities($comment); 
+0

二重引用符と一重引用符をフラグで置き換える必要があるため、エンコード後にエンコードされていないものに戻すことができます。この葉はまだコード化されています – jkushner

+0

更新された質問 – jkushner

+0

@jkushner – mkHun

関連する問題