Reputation: 276
I am trying to extract all Image Names and their Paths from a file that was uploaded. I am looking for the proper regex.
The string is massive but a section would look like:
..... [{\"name\":\"Aluminum_Black_mat\",\"id\":\"b3281b09-88be-4e9b-9fc2-514cbaae0a0e\",\"backFaceCulling\":true,\"wireframe\":false,\"ambient\":[0.0,0.0,0.0],\"diffuse\":[1.0,1.0,1.0],\"specular\":[0.0,0.0,0.0],\"emissive\":[0.0,0.0,0.0],\"specularPower\":2.56,\"alpha\":1.0,\"diffuseTexture\":{\"name\":\"BlackRailing.jpg\",\"level\":1.0,\"hasAlpha\":false,\"getAlphaFromRGB\":false,\"coordinatesMode\":0,\"isCube\":false,\"uOffset\":0.0,\"vOffset\":0.0,\"uScale\":1.0,\"vScale\":1.0,\"uAng\":0.0,\"vAng\":0.0,\"wAng\":0.0,\"wrapU\":1,\"wrapV\":1,\"coordinatesIndex\":0,\"isRenderTarget\":false,\"renderTargetSize\":0,\"mirrorPlane\":null,\"renderList\":null,\"animations\":[]},\"diffuseFresnelParameters\ .... ETC
In that string there is BlackRailing.jpg. That's what i need to get. If it had a path i would like to get that too. Bascially everything between those quotes and only if it is an image. I have been mucking around on http://www.regexr.com/ for a while and i can't get it to match perfectly. And when i run the following i always get everything before the first quote.
preg_match_all('/[ :]".*.(jpe?g|png|bmp)/i', $buffer, $matches);
I am really bad with regex, any Help with the right regular expression for php would be greatly appriciated.
Thanks!
Upvotes: 2
Views: 668
Reputation: 627536
As you treat a JSON file as a text file, you can use a preg_replace approach like this assuming that there are no spaces between :
and the value:
preg_match('/(?<=:")[^"]*\.(?:jpe?g|png|bmp)/i', $buffer, $matches);
See regex demo
The (?<=:")
look-behind just checks for :"
but does not capture it.
Note if you have a single space in-between the :
and "
, you can use '/(?<=:[ ]")[^"]*\.(?:jpe?g|png|bmp)/i'
regex, but if there may be more, you will need a capturing group approach.
Upvotes: 3