regex - Regular Expression to detect and escape multiple single quotes, other than reserved by JSON -
i want have regular expression may detect , escape multiple single quotes double black slash (\\). example if there ' should become \\'
challenge here that:
1) should not escape single quotes used json.
example below:
{'key1':'value1','key2':'value2'} it should not escape single quotes covering keys , values. in above example, none of quotes should escaped. single quotes inside values should escaped.
2) should escape multiple single quotes present there inside value (in key value pair).
here challenge string can used example:
challenge string:
{'addressusageid':''asd'','edit':'edit','siteusage':'bi'llto','paymentterm':'asd','salesperson':'s'a@,#$'%^''&*'()<>?`~','language':'','primaryusage':''''','internallocation':'t'est'} it should escaped below:
{'addressusageid':'\'asd\'','edit':'edit','siteusage':'bi\'llto','paymentterm':'asd','salesperson':'s\'a@,#$\'%^\'\'&*\'()<>?`~','language':'','primaryusage':'\'\'\'','internallocation':'t\'est'}
single quotes not valid json. if pull string through jsonlint, show that. proper way of making json string in php using json_encode() on array or object. automatically escape quotes if need escaped.
as far problem goes. use following pseudocode:
$s = $json_string without first 2 , last 2 characters #$a array of "key':'value" $a = explode( $s, "','" ); foreach( $a $i => $keyvalue ) { $temp = explode( $keyvalue, "':'" ); #now replace instances of ' \' $temp = str_replace( "'", "\'", $temp ); #now fancy stitch together. }
Comments
Post a Comment