-1

Nearly, when i write code with preg_match.Like this

    $string = <<<ETO
    <svg "[^>]*? width="480.24px" height="360px" viewBox="0 0 480.24 360" enable-background="new 0 0 480.24 360" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    ETO;

    preg_match('/<svg[^>]*?width=[\'|"]{1}(.*?)[\'|"]{1}[^>]*?>/is', $string, $svginfo);

the $svginfo got null.

Sorry about this. I know [^>]*? should not be there in svg. But when someone posts this I need to get the width info from this content.

Can anyone explain how to do this?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • i suspect you need to add some escaping –  Jul 06 '17 at 05:47
  • [^>]*? in $string. it's just a accident – user3575475 Jul 06 '17 at 05:55
  • 1
    I have to delete the `[^>]*?` from `$string` to get a match - if you don't mean it to be there, please edit the question. – Ken Y-N Jul 06 '17 at 05:59
  • show us the real question or dont bother –  Jul 06 '17 at 06:11
  • remove [^>]*? from $string will be matched ok. It's just a example.AND FOR MORE. when users post some content like this,and you need to do some preg on content.How do this ? – user3575475 Jul 06 '17 at 06:17
  • Please do not edit crucial bits of the question - you've changed the regex (but not the `$string` corruption), yet in my answer I showed you how to fix the original regex, and the second match would be the width you were searching for. – Ken Y-N Jul 06 '17 at 06:34
  • Sorry, i just want to explain problem more detailed. I know how to get the width info.What i care is Why $string contains [^>]*? will be error – user3575475 Jul 06 '17 at 07:04

1 Answers1

0

Testing out on an online regex tester, it tells me:

\\ matches the character \ literally (case insensitive)
1 matches the character 1 literally (case insensitive)

If instead I use \1 in the middle of the string, it says:

\1 matches the same text as most recently matched by the 1st capturing group

This means that you will find the matching closing quote, which is what you seem to want to do.

Oh, and Zalgo.


UPDATE:

Ah, this is the Zalgo problem. The problem is that you want to ensure you don't skip over the whole <svg .... > tag, so that <svg>width="99" is rejected. A simple and valid HTML pattern would be:

<svg name=">" width="42">

A regex to catch this and similar variants will soon get out of hand; the proper way to do this is use an HTML parser; here is one article about how to do it.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114