Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Everything posted by effigy

  1. I recommend an XML parser if you're doing other work; otherwise: <pre> <?php $data = <<<DATA IE --> <SSN>111-11-1111</SSN> --> <SSN> 111-11-1111</SSN> --> <SSN> 111-11-1111 </SSN> --> <SSN> 111-11-1111 </SSN> DATA; $data = preg_replace('%<SSN>\s*\d{3}-\d{2}-(\d{4})\s*</SSN>%', '<SSN>XXX-XX-$1</SSN>', $data); echo htmlspecialchars($data); ?> </pre>
  2. Do you mean intermediate? If so, what makes this the case? If no special conditions are needed, see str_replace.
  3. <pre> <?php $value = '#A#'; $row = array('A_col' => 'ABC'); echo $value = preg_replace('/#([A-Z])#/e', '$row["${1}_col"];', $value); ?> </pre>
  4. Do mean non-Latin-1 or non-ASCII? What is the data encoded in? UTF-8, CJK...?
  5. Much of this can be improved by using arrays and the static replacements can use str_replace.
  6. No. Perhaps you're just impatient? <pre> <?php $text = '<:\ >:\\'; $text = str_replace('<:\\', '<img src="embarrassed.gif">', $text); $text = str_replace('>:\\', '<img src="angry.gif">', $text); echo $text; ?> </pre>
  7. Do you have samples of these?
  8. Some additions: Added shorthands There's no need to escape . inside of a character class Added a possessive to prevent backtracking before @ Changed enclosing quotes to avoid unnecessary escapes Removed unnecessary parentheses <pre> <?php $text = '123456789, 12345, xxx.sss@some.net'; $text = preg_replace( '/([^@\s]++)@([^\s.]+)\.(\S+)/i', '<a href="mailto:$1[at]$2[dot]$3">$1[at]$2[dot]$3</a>', $text ); $text = preg_replace( '/(\d{5})(\d{4})/', '$1-$2', $text ); echo $text; ?> </pre>
  9. Actually, do you have a list of what characters are valid? In regex you could use ranges to cover this basis.
  10. Such as this? Have you tried a hex editor? Are you on Unix?
  11. Sure thing. See the Resources post. Personally, I fancy http://www.regular-expressions.info.
  12. Are there no other attributes you need to retain? <pre> <?php $data = <<<DATA <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> DATA; $data = preg_replace('/<p[^>]+class="head-styles[^>]+>/', '<p myvalue>', $data); echo htmlspecialchars($data); ?> </pre>
  13. Something like this? <pre> <?php $data = <<<DATA <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> DATA; $data = preg_replace('/(<p[^>]+class="head-styles)[^"]+/', '$1-new_style', $data); echo htmlspecialchars($data); ?> </pre>
  14. In today's world I prefer to work everything in UTF-8; although, I rarely use Korean, Chinese, or Japanese and I hear these are preferable in other encodings. UTF-8 overlaps ISO-8859-1 (Latin-1), so if that's all you have, changing it should be transparent.
  15. Not PHP specifically; however, PHP uses PCRE (Perl Compatible Regular Expressions) which are practically the standard. The PHP docs do cover these, but not well in my opinion. I prefer http://www.regular-expressions.info.
  16. Where is the pound sign coming from? $_POST? Did you clear your cache? Is the server enforcing UTF-8?
  17. You're working in UTF-8: http://www.phpfreaks.com/forums/index.php/topic,163936.0.html What's in your meta tag?
  18. The encoding of which text? See iconv.
  19. Are you using this on multiple file names? If so, and most of them appear towards the end, this pattern will suit you well: <pre> <?php preg_match('/^.+__(.+?)(?=\.flv)/', '9aca1b0bed3396d01e082c113f65971__e67056a3a1549aa5211b3ad7e212039f__004d.flv.flv.flv', $matches); print_r($matches); ?> </pre>
  20. http://www.phpfreaks.com/forums/index.php/topic,137431.msg585346.html#msg585346
  21. A character class ([...]) matches any one item that it contains, and, since you have the quantifier inside of the class, this pattern will match / or * once. Because character classes have their own set of metacharacters. Outside of a character class ^ serves as an anchor, inside it serves as negation. If you want to match all beginning and end tags /<[^>]+>/ will suffice.
  22. There's a discussion on Perl here.
  23. From php.net: $html is not an array, so you would not use preg_grep. You want to find all occurrences, so you would use preg_match_all since it indicates that it is global. Use str_replace if the string is static; if not, use preg_replace.
  24. What metacharacters? echo preg_replace('/[^\da-z.]/', '', $string);
  25. What about hyphenated last names, suffixes, or two-word first names?
×
×
  • 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.