-1

I need some help with a problem if possible.

<IndexFile index="dlc:blahblahblahblah.zip" version="1.19.0" />

How would I use php to remove everything in the above line of code except

blahblahblahblah.zip

Note: blahblah isn't actual name, the name changes; I just need to remove the xml on either side of the .zip file.

I've tried a few things like strip_tags() but nothing works up to now.

unknown1
  • 15
  • 4
  • can you provide actual xml that you want to remove. This information is insufficient for providing answer – Alankar More Mar 11 '16 at 06:57
  • By using `preg_quote` on your `$find` variable you ruin any regex in it and every character will be considered that exact character (including `(.+?)`) – h2ooooooo Mar 11 '16 at 08:12
  • @unknown1 You can still use `preg_quote`, just don't use it on stuff you want to regex. `$pattern = '/^.*' . preg_quote('index="dlc:', '/') . '(.+?).*$/m'`; – h2ooooooo Mar 11 '16 at 08:41

2 Answers2

1

Your test string was <IndexFile index="dlc:blahblahblahblah.zip" version="4.19.0" />

Your regex pattern should be : index="dlc:(.+?)".

Your answer is: blahblahblahblah.zip

Try it out at https://regex101.com/

See this answer for greedy vs nongreedy matching: java-pattern-does-not-return-leftmost-match

Community
  • 1
  • 1
roadrunner66
  • 7,772
  • 4
  • 32
  • 38
0

Ideally, you have to use an XML parser to parse the file and use XPaths/looping to get that item.

If that is the only line, why can't you just use a Regex to extract the value? index="[a-zA-Z0-9_.-:]*" (or [a-zA-Z0-9_.-:]*.zip)?

Again, you need to think about the future impacts.

Rajan Panneer Selvam
  • 1,279
  • 10
  • 24