
effigy
Staff Alumni-
Posts
3,600 -
Joined
-
Last visited
Never
Everything posted by effigy
-
Beautiful Barand. I forgot about that gem.
-
The short answer: We don't need to capture anything. They're required for the quantifier. The long answer: "[G]rouping-only parentheses... don't capture, but just group regex components for alternation and the application of quantifiers. .... They can also be efficient -- if the regex engine doesn't need to keep track of the text matched for capturing purposes, it can work faster and use less memory. .... Non-capturing parentheses are useful when building up a regex from parts." -- Mastering Regular Expressions
-
<pre> <?php $num = '381631234567'; echo preg_replace('/\A\d{3}(\d{2})(\d{3})(\d{4})\z/e', 'sprintf("(%03d) %3d-%4d", $1, $2, $3)', $num); ?> </pre>
-
Keep it simple: /\[color:([^\]]+)\]/i.
-
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <pre> <?php $data = '0xe9 0x98 0xbf 0xe4 0xb8 0x8d 0xe6 0x9d 0xa5 0xe6 0x8f 0x90 0x2e 0xe9 0x98 0xbf 0xe4 0xb8 0x8d 0xe9 0x83 0xbd 0xe7 0x83 0xad 0xe8 0xa5 0xbf 0xe6 0x8f 0x90'; ### Separate. $codes = preg_split('/\p{Z}+/', $data); ### Convert to characters. $result = ''; foreach ($codes as $code) { $result .= pack('C', $code + 0); } echo $result, '<hr>'; ### Separate into characters. print_r(preg_split('/(?=\p{L})/', $result, -1, PREG_SPLIT_NO_EMPTY)); ?> </pre>
-
<pre> <?php $tests = array( '1,2', 'a,b,c', ',1,2,3', '2,10,10000,9,0', ',', ',9', '10,', '1,a,3', '1,,100', '1,2,3,4,5,6,7', '21,532,123,7665,12,34,23,6,3' ); foreach ($tests as $test) { echo "$test — "; echo preg_match('/\A(?:\d+,)+\d+\z/', $test) ? 'Valid' : 'Invalid' ; echo '<br>'; } ?> </pre>
-
Your replacement is being interpreted before the evaluation process--swap the quoting: '$this->components["$1"]'.
-
An aside from the main post: Utilize the fact that str_replace accepts arrays to clean up your code, or use regex. For example, preg_replace('/\[(\/?h\d)\]/', '<$1>', $str); will take care of all the h# tags, opening and closing. You can expand the h into a character class or alternation to handle the other tags.
-
echo date('l F d<\s\u\p>S</\s\u\p>, Y \a\t g:i a', time());
-
Use "l F dS, Y \a\\t g:i a".
-
<pre> <?php $data = <<<DATA Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam dictum, lacus euismod aliquet vehicula, tellus eros venenatis massa, ultrices aliquam magna massa ut felis. Suspendisse eget eros. Morbi malesuada neque in felis. Quisque volutpat. Donec eu elit vel velit dapibus aliquet. Sed condimentum sagittis est. Ut ultrices, ipsum eget feugiat vestibulum, nibh orci mollis purus, id blandit metus odio sollicitudin lorem. In hac habitasse platea dictumst. Integer erat. Proin blandit. Vestibulum dignissim neque et mi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum ullamcorper, diam ut tincidunt facilisis, erat erat posuere tortor, id pellentesque velit elit ut mauris. Fusce at odio quis pede tincidunt tincidunt. Vivamus mollis vulputate lacus. Duis consequat. Aliquam posuere leo non metus commodo dictum. Donec iaculis consectetuer pede. Nulla facilisi. In sed augue. Suspendisse commodo, massa elementum pellentesque varius, justo nisi lobortis est, id placerat magna felis in tellus. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec pharetra risus quis quam. Nullam elit arcu, scelerisque at, cursus quis, varius non, leo. Suspendisse non nibh at metus tempor dictum. Mauris ante odio, pellentesque ac, consectetuer consectetuer, porta ac, nisl. In hac habitasse platea dictumst. Aliquam sit amet elit vel magna lacinia tristique. Nullam justo. Etiam hendrerit molestie massa. Morbi risus nisi, commodo facilisis, vulputate ac, hendrerit mollis, massa. Suspendisse adipiscing odio a enim. Aenean id erat vel orci hendrerit gravida. Quisque semper elementum tellus. Vestibulum metus libero, fermentum sit amet, facilisis ut, facilisis et, nulla. DATA; $pieces = preg_split('/(.{390,410})(?=\s)/s', $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($pieces as $piece) { $piece = trim($piece); echo '<b>', strlen($piece), '</b>: ', $piece, '<hr>'; } ?> </pre>
-
WHAT IS => I Cannot find it on the internet!
effigy replied to freedomflyer's topic in Miscellaneous
That's enough. Locked. -
I get: <strong>bllaaaahhhhhh</strong>.
-
Correct. Although, I want to clarify what you mentioned about the expression stopping. Yes, the laziness portion stops matching data when it is fulfilled and the following expressions (if any) are sufficed, but the expression as a whole matches only once (stops) because this is the behavior of preg_match. One must use preg_match_all to match every instance of the pattern. Adding this before print_r should be helpful: foreach ($matches as &$match) { $match = htmlspecialchars($match); } Per the docs, index 0 is the full match, while indexes 1 and above are the individual parenthetical captures.
-
They are similar, but MySQLi opens you up to newer features of MySQL and the world of OOP, which is extremely useful. A query and fetch is demonstrated here.
-
What string are you matching this against? Word boundaries should suffice: /\bNews\b/.
-
WHAT IS => I Cannot find it on the internet!
effigy replied to freedomflyer's topic in Miscellaneous
I've seen it referred to as a "fat arrow" or "fat comma" in the Perl community. -
You can see the problem greediness creates by adding more data and modifying the pattern: <pre> <?php $data = <<<DATA <td> <h3>00CA - Goldstone (GTS)</h3> United States </td> <td> <h3>00CA - Goldstone (GTS)</h3> United States </td> DATA; preg_match('%</h3>(.+)</td>%s', $data, $matches); print_r($matches); ?> </pre> . plus the /s modifier is going to match anything. When greedy, it has no concern for following patterns until it is done. Therefore, (.+) is going to match the rest of the string, then come to </td> and realize that it needs to give away its matches one by one (backtrack) in order to try and finish the match. Effectively, this means that the last </td> that was gobbled by (.+) is going to match. Laziness, on the other hand, is going to take one, then make sure it's not taking from the following pattern, then repeat the process. For example, (.+?) takes the "U" then makes sure "</td>"* isn't next; it's not, so it grabs the "n", checks, then the "i", checks, and so forth, all the way up to the tab before "</td>". * Actually, it's only going to make sure "<" isn't next. If it is, then it would it would look for "/" and so forth. The same applies throughout: "</td>" is not an atomic unit as far as the regex is concerned. It deals with the characters one at a time.
-
[SOLVED] Count the amount of days since (x) minus weekends
effigy replied to gijew's topic in PHP Coding Help
Does this work? <pre> <?php function weekdays_since($date) { $from_date = strtotime($date); $to_date = time(); $days = 0; while ($from_date < $to_date) { $from_date = strtotime('+1 day', $from_date); $day = date('D', $from_date); if ($day == 'Sat' || $day == 'Sun') { continue; } ++$days; } return $days; } echo weekdays_since('01/01/2008 12:00 AM'); ?> </pre> -
Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace. Signed, A peep.
-
preg_match only matches, but preg_replace matches and replaces (what it matches). Essentially, what you have is... if (my string matches the pattern) { run the replace against the pattern } ...which is pointless because you cannot replace what you cannot match--all you need is the "run the replace against the pattern" step. <<< is heredoc syntax.
-
The code was demonstrative rather than literal. Please read the manual for preg_match. Also, try echo $matches[1];.
-
The code I provided does not work?
-
Is it as simple as the country is always between the h3 and td? If so, I guess we're done