Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Posts posted by effigy

  1. With a flexible approach to a "number" or "word":

     

    <pre>
    <?php
    $str = '>< > < >123.< > 999 < >8,88< >eight< > eig.ht < >ni.ne< > nine <';
    preg_match_all('#>\s*(?:[\d,.]+(?<!\.)|[a-z.]+)\s*<#is', $str, $matches);
    print_r($matches);
    ?>
    </pre>
    

  2. You're matching HTML, so you're not going to see it if you print it--the browser is parsing it.

     

    <pre>
    <?php
    $str = '</td> </tr> </table>';
    preg_match('%</td>\s*</tr>\s*</table>%', $str, $matches);
    foreach ($matches as &$match) {
    	$match = htmlspecialchars($match);
    }
    print_r($matches);
    ?>
    </pre>
    

  3. At some point the . was \S to only capture non-whitespace. I can't figure out why this changed in the old posts, but that's what you're after. I've restored this and cleaned up the img addition with alternation:

     

    preg_match_all('%
          (
             (?>
                ### Protocol or start.
                (?:
                   (??:https?|ftp)://)
                   |
                   www\.
                )
                ### Body: gobble non-space, [/url], [/img]
                (??!\[/(?:url|img)\])\S)+
                ### Avoid ending punctuation.
                (?<!\s\p{P})
             )
             ### Not followed by an url/img end.
             (?!\[/(?:url|img)\])
          )
          %x', $str, $matches);
    

  4. 1. You're only going to catch double-quoted attributes.

    2. You haven't accounted for rogue white space.

    3. There are various modules on CPAN for things like this; my take is below:

     

    use strict;
    use warnings;
    use XML::Twig;
    use Data::Dumper;
    
    my $data = <<HTML;
    <img src="foo" height="1" />
    <img src="testing!" height="50" width="200" />
    <img height="20" src="lol" />
    HTML
    
    my @atts;
    my $twig = XML::Twig->new(
    twig_roots => {
    	'img' => sub {
    		push @atts, $_->atts();
    	}
    }
    );
    $twig->parse_html($data);
    print Data::Dumper->Dump([\@atts]);
    

  5. <pre>
    <?php
    $data = <<<DATA
       <p><em class="example">
    			                     3.30
    							</em></p>
    DATA;
    preg_match('/<em class="example">\s*(\d{1,2}.\d{2})/', $data, $matches);
    array_shift($matches);
    print_r($matches);
    ?>
    </pre>
    

  6. <pre>
    <?php
    $tests = array(
    	### Pass.
    	'username:secret@host',
    	'username:secret:username@host',
    	'username:secret@host:000/username',
    	'username:secret:username@host:000/username',
    	### Fail.
    	'username:secret@host/',
    	'username:secret:username:etc@host',
    	'username:secret@host:username',
    	'username@host:000/username',
    	'username:secret:username@host:000@username',
    );
    foreach ($tests as $test) {
    	echo $test, ' <b>';
    	echo preg_match('%
    		\A
    		[^:@/]+:[^:@/]+(?::[^:@/]+)?
    		@[^:@/]+
    		(?::\d+/.+)?
    		\z
    	%x', $test) ? 'Pass' : 'Fail';
    	echo '</b><br/>';
    }
    ?>
    </pre>
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.