2

I'm trying to match the height field in the svg tag of an svg file that can look like this (multiline):

<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
id="svg2"
height="1052.36220472"
width="744.094488189">

This regex gives me almost what I want:

/(<svg[^<>]*height=")(\d*\.\d*)("[^<>]*>)/ms

I'd like to match only the float value for the height (1052.36220472), so lookahead/lookbehind seems the way to go, but I cannot use non-fixed width lookbehinds, so

/(?<=<svg[^<>]*height=")(\d*\.\d*)(?="[^<>]*>)/ms

does not work - what can I do instead?

I want to use the match for replacement within php, with the preg_replace() function.

revo
  • 47,783
  • 14
  • 74
  • 117
snurden
  • 173
  • 5

1 Answers1

0

As mentioned in the comments by revo (thanks!), \K does the job:

/(?<=<svg[^<>]*height=")\K(\d*\.\d*)(?="[^<>]*>)/ms

Will only match the decimal number and drop the rest of the svg tag.

snurden
  • 173
  • 5