mstdmstdd Posted June 21, 2017 Share Posted June 21, 2017 Hello!Есть строка я рядом картинок где формат последней картинки отличается Edit by Psycho: Translation: There is a line next to the pictures where the format of the last picture differs $str= ‘ <figure class="article-body-content-main-photo full-width"> <img sizes="(max-width: 800px) calc(100vw - 20px), (max-width: 1280) 55vw, 700px" srcset= "//site.com/article-leads-horizontal-261/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 261w, //site.com/article-leads-horizontal-437/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 437w, //site.com/article-leads-horizontal-800/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 800w, //site.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 700w, //site.com/article-leads-horizontal-1400/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 1400w, " src="//site.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png" alt="" />’; And using preg_match_all with $pattern = '~ //[\s]* # symbol with possible backspace ([\w\.\/\-]+) # get list of images of article from next article html (?: [\s]* ([\w]+) #get dimention size of image [\s]* ) | # condition for last image " ~isx'; I got results: [0] => Array ( [0] => //img.wennermedia.com/article-leads-horizontal-261/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 261w [1] => //img.wennermedia.com/article-leads-horizontal-437/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 437w [2] => //img.wennermedia.com/article-leads-horizontal-800/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 800w [3] => //img.wennermedia.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 700w [4] => //img.wennermedia.com/article-leads-horizontal-1400/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png 1400w [5] => " [6] => " [7] => //img.wennermedia.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [8] => " ) [1] => Array ( [0] => img.wennermedia.com/article-leads-horizontal-261/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [1] => img.wennermedia.com/article-leads-horizontal-437/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [2] => img.wennermedia.com/article-leads-horizontal-800/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [3] => img.wennermedia.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [4] => img.wennermedia.com/article-leads-horizontal-1400/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.png [5] => [6] => [7] => img.wennermedia.com/article-leads-horizontal/screen-shot-2017-06-19-at-23406-pm-73259c31-a395-463b-8eaa-bf4f9152d9aa.pn [8] => ) [2] => Array ( [0] => 261w [1] => 437w [2] => 800w [3] => 700w [4] => 1400w [5] => [6] => [7] => g [8] => ) All images got ok, but last not.Which is the valid way ?Thanks! Link to comment Share on other sites More sharing options...
dalecosp Posted June 21, 2017 Share Posted June 21, 2017 For the reader: first line is "There is a line i next to the pictures where the format of the last picture differs". Link to comment Share on other sites More sharing options...
Psycho Posted June 21, 2017 Share Posted June 21, 2017 @mstdmstdd Not sure I understand your problem. There are six (6) images in the content if the variable $str and there are six images in your result array. Is your problem the fact that "g" is returned as the 2nd parameter for the last image? There is no dimension in the source for that image, so you won't get back a valid dimension anyway. Are you just wanting to get an empty value for that then? EDIT: Ah, sorry, I now see the "g" was being stripped off the end of the image name. Link to comment Share on other sites More sharing options...
Psycho Posted June 21, 2017 Share Posted June 21, 2017 I'm sure your expression can be written more simply. But, your problem can be resolved by removing all the line-breaks, spaces and comments you used to help make the expression easier to read. You can always add several PHP comment lines before or after the expression definition to explain the different parts of the expression. $pattern = '~//[\s]*([\w\.\/\-]+)(?:[\s]*([\w]+)[\s]*)*| "~isx'; Link to comment Share on other sites More sharing options...
mstdmstdd Posted June 22, 2017 Author Share Posted June 22, 2017 Thank you for your explanations! But did you mean that this way of pattern $pattern = '~ //[\s]* # symbol with possible backspace ([\w\.\/\-]+) # get list of images of article from next article html ( [\s]* ([\w]+) #get dimention size of image [\s]* )* | # condition for last image [\s]" ~isx'; with x key can bring to wrong working of expression ? Link to comment Share on other sites More sharing options...
dalecosp Posted June 22, 2017 Share Posted June 22, 2017 Did you remove the "?" from your pattern in your second post? At any rate, he's right, it works with $pattern in a single line as he typed it. I think this might be what got you: Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.