Regex replacement with multiple occurances and different values within a string in PHP -
i have code:
<object height="510" width="650">height="510"width="650"> or code:
<objectheight="345"width="123'> height='456'width=789> the quotes around values double, single or none. , words put or there 1 or more whitespaces. , important digits value anything
so, need replace integervalue in height="510" (or height='123' or height=456) own variable $height_val.
my code far:
$height_val = "640"; $pattern = ??? // regex need find out $replacement = $height_val; $string = '<objectheight="345"width="123\'> height=\'456\'width=789>' $result = preg_replace($pattern, $replacement, $string); and final result should <objectheight="640"width="123\'> height=\'640\'width=789>
the reg-ex i'd use is: height=(["']*)[0-9]*(["']*). ensures height value non-alpha digit after equals followed length number.
$height_val = "640"; //$pattern = '/height=\d[0-9]*\d/' // pre comments regex $pattern = height='/(["']*)[0-9]*(["']*)/'; //new regex $replacement = $height_val; $string = '<objectheight="345"width="123\'> height=\'456\'width=789>' $result = preg_replace($pattern, $replacement, $string); i've tested on following variables:
<object height="510" width="650">height="510"width="650"> <objectheight="510" width="650">height="510"width="650"> <object height='510' width="650">height="510"width="650"> <objectheight='510'width="650">height="510"width="650"> value="width=650&height=515&plugins=http:// going forward recommend try using regex tester try own combinations. can use regex reference give character classes.
updated: if wish allow no quotation marks use following:
Comments
Post a Comment