Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Posts posted by effigy

  1. <pre>
    <?php
    $start = '<td class="object_desc" dir=rtl align=right valign=top background="iaasds/jxx05.gif" width="554" height="67" style="padding-right:20px;padding-left:35px;">';
    $end = '</td>';
    preg_match('/' . preg_quote($start) . '(.*?)' . preg_quote($end) . '/', $data, $matches);
    print_r($matches);
    ?>
    </pre>
    
    

  2. <pre>
    <?php
    $data = <<<DATA
    (great,cool) site, I (like,enjoyed) it
    i love (apples,oranges,bananas,grapes,tangerines,cake,pie)
    DATA;
    
    echo preg_replace_callback(
    	'/\((.*?)\)/', 
    	create_function(
    		'$words',
    		'$words = split(",", $words[1]);
    		 return array_rand(array_flip($words));'
    	),
    	$data
    );
    ?>
    </pre>
    

  3. On second thought...

     

    <pre>
    <?php
       $str = <<<STR
    <div class="s_ach_stat">10<img src="../images/achievements/tiny_shield.gif">
                  (01-13-2009)
               </div>
    <span>Explore Storm Peaks</span><span class="achv_desc">Explore Storm Peaks, revealing the covered areas of the world map.</span>
    STR;
       $pieces = preg_split('#[()]|\s*<[^>]*>\s*#', $str, -1, PREG_SPLIT_NO_EMPTY);
       print_r($pieces);
    ?>
    </pre>
    

  4. What was wrong with nrg's solution? Below is my simplification: the expression is condensed and the looping is by reference, making the appearance a little more kind.

     

    <pre>
    <?php
    $str = <<<STR
    <div class="s_ach_stat">10<img src="../images/achievements/tiny_shield.gif">
                  (01-13-2009)
               </div>
    <span>Explore Storm Peaks</span><span class="achv_desc">Explore Storm Peaks, revealing the covered areas of the world map.</span>
    STR;
    $pieces = preg_split('#\s*<[^>]*>\s*#', $str, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($pieces as &$piece) {
    	$piece = trim($piece, '()');
    }
    print_r($pieces);
    ?>
    </pre>
    

  5. preg_split splits a string by expression, discarding the parts that it matches. This expression has two delimiters (//), a character class ([]), a quantifier (+), a shorthand (\s), and two literals (()).

     

    The delimiters contain the expression; the character class matches any one character inside, but the plus quantifier instructs it to match 1 or more; and what we're asking it to match is either "(", ")", or any kind of whitespace (\s).

  6. Try this:

     

    <pre>
    <?php
    $data = <<<DATA
    <h1>Heading
    </h1>
    <pre>Pre
    </pre>
    <ul>
    <ol>A
    </ol>
    <ol>B
    </ol>
    </ul>
    <p>P
    </p>
    DATA;
    
    ### Separate the tags from the data.
    $pieces = preg_split('%(<[^>]*>)%', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
    	echo htmlspecialchars($piece);
    }
    echo '<hr>';
    
    ### Parse.
    $level = 0;
    foreach ($pieces as &$piece) {
    	echo "($level) ", htmlspecialchars($piece);
    	### Determine nesting.
    	if (substr($piece, 0, 1) == '<') {
    		preg_match('%<(?:pre|[ou]l)[^>]*>%i', $piece) ? ++$level : null ;
    		preg_match('%</(?:pre|[ou]l)>%i', $piece) ? --$level : null ;
    	}
    	### If a level exists, trim trailing white space.
    	elseif ($level) {
    			$piece = rtrim($piece);
    	}
    }
    unset($piece);
    echo '<hr>';
    foreach ($pieces as $piece) {
    	echo htmlspecialchars($piece);
    }
    ?>
    </pre>
    

  7. This is only a basis as unpaired single quotes (apostrophes) may throw off the detection.

     

    <pre>
    <?php
    ### $'s escaped for sake of example.
    $data = <<<DATA
    ##div('\$\$title or "\$\$title"');
    // current results: '\$\$title or "\$view->title"'
    // expected results: '\$\$title or "\$\$title"'
    
    ##div("\$\$title or '\$\$title'");
    // current results: "\$view->title or '\$view->title'"
    // expected results: "\$view->title or '\$view->title'"
    DATA;
    
    echo '<b>Original:</b><hr>';
    echo $data;
    
    echo '<br><br><b>Split on Double Quote Pairs:</b><hr>';
    $pieces = preg_split('/(".*?")/s', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    print_r($pieces);
    
    echo '<br><br><b>Double Quote Replacement:</b><hr>';
    $singles = 0;
    foreach ($pieces as &$piece) {
    	$singles += substr_count($piece, "'");
    	### No replacement should be made if:
    	### 1. The line does not start with a double quote; and
    	### 2. If the number of single quotes are not even (closed).
    	if (!substr($piece, 0, 1) == '"' || $singles % 2) {
    		continue;
    	}
    	$piece = preg_replace('/"(.*?)"/s', '"====="', $piece);
    }
    print_r($pieces);
    
    echo '<br><br><b>Reassembled:</b><hr>';
    echo join('', $pieces);
    
    ?>
    </pre>
    

  8. <pre>
    <?php
    $content = file_get_contents('http://www.bungie.net/stats/halo3/fileshare.aspx?gamertag=HaLo2FrEeEk');
    $pieces = explode('<div class="shareTitle">', $content);
    foreach ($pieces as $piece) {
    	if (!preg_match('%/images/halo3stats/fileshareiconssm/film(?:clip)?s/sm/%', $piece)) {
    		continue;
    	}
    	preg_match('%
    		fileshareitem_titleLink[^>]+?
    		h3fileid=(?P<h3fileid>\d+)
    		[^>]+>
    		(?P<title>.+?)
    		</a>
    		.+?
    		Film\s+Length:\s+
    		(?P<length>[\d:]+)
    	%xis', $piece, $matches);
    	$result = array();
    	foreach (array_keys($matches) as $key) {
    		if (!is_numeric($key)) {
    			array_push($result, $matches[$key]);
    		}
    	}
    	print_r($result);
    
    }
    ?>
    </pre>
    

  9. You may want to try some other tutorials via our resources.

     

    These two paragraphs from your link cover character classes and quantifiers:

     

    To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].

     

    The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times".

     

    And here's a breakdown of the pattern:

    NODE                    EXPLANATION

    ----------------------------------------------------------------------

      (                        group and capture to \1:

    ----------------------------------------------------------------------

        "                        '"'

    ----------------------------------------------------------------------

        [^"]+                    any character except: '"' (1 or more

                                times (matching the most amount

                                possible))

    ----------------------------------------------------------------------

        "                        '"'

    ----------------------------------------------------------------------

      )                        end of \1

    ----------------------------------------------------------------------

     

×
×
  • 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.