Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Everything posted by effigy

  1. Look through the Google results I linked. Perhaps this one?
  2. Do you want your original XML to remain exactly the same, aside from the sorting factor? In XSLT you can control the format--it can be whatever you want. If you want to leave the original XML structure the same, but make a few changes, you use an identity transform.
  3. I cannot get XSLT to function on my PHP install at work. I'm also rusty. See if this helps or other items here.
  4. <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>
  5. You don't have to modify the XML. See example #2 here and the example here. If you have trouble, please post a larger XML sample and your code.
  6. You've already taken care of the thanks part. Clicking the "TOPIC SOLVED" button in the bottom left is all that's left.
  7. <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>
  8. <pre> <?php $data = <<<DATA Great site, I like it Cool site, I like it DATA; print_r(preg_split('/site,\s+I\s+|\s+it(?:\s+|\z)/', $data, -1, PREG_SPLIT_NO_EMPTY)); ?> </pre>
  9. Without the proper tools, nesting is always an issue. What if the tag has data, do you want to close it rather than remove it?
  10. 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>
  11. 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>
  12. My guess is to accommodate those coming from other languages. For example, Perl uses join rather than implode, so I'd much rather use join across the board than remember the idiosyncrasies.
  13. 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).
  14. <pre> <?php $str = 'Nick Leigh(1)'; print_r(preg_split('/[()\s]+/', $str, -1, PREG_SPLIT_NO_EMPTY)); ?> </pre>
  15. effigy

    Google REGEX

    Does Google not have an API for this? Try %<a[^>]+?href="http://[^"]+"[^>]+?class=l\s%. You can reduce the results with array_slice.
  16. 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>
  17. This is an easy task, but nesting would complicate things. Is there a lot of nesting? How big are these documents?
  18. $pieces = preg_split('/\s{2,}/', $data, -1, PREG_SPLIT_NO_EMPTY); echo join(',', $pieces);
  19. Can you not incorporate that into your pattern?
  20. 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>
  21. <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>
  22. You may want to try some other tutorials via our resources. These two paragraphs from your link cover character classes and quantifiers: 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.