effigy
Staff Alumni-
Posts
3,600 -
Joined
-
Last visited
Never
Everything posted by effigy
-
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>
-
Do you mean intermediate? If so, what makes this the case? If no special conditions are needed, see str_replace.
-
<pre> <?php $value = '#A#'; $row = array('A_col' => 'ABC'); echo $value = preg_replace('/#([A-Z])#/e', '$row["${1}_col"];', $value); ?> </pre>
-
Do mean non-Latin-1 or non-ASCII? What is the data encoded in? UTF-8, CJK...?
-
Much of this can be improved by using arrays and the static replacements can use str_replace.
-
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>
-
[SOLVED] preg_replace/ pulling mysql data for zip code and e-mail
effigy replied to BandonRandon's topic in Regex Help
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> -
[SOLVED] Looking for a special character chart/table
effigy replied to gmcmudder's topic in PHP Coding Help
Actually, do you have a list of what characters are valid? In regex you could use ranges to cover this basis. -
[SOLVED] Looking for a special character chart/table
effigy replied to gmcmudder's topic in PHP Coding Help
Such as this? Have you tried a hex editor? Are you on Unix? -
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>
-
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>
-
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.
-
[SOLVED] preg_match, preg_match_all and preg_grep
effigy replied to diamondnular's topic in Regex Help
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. -
Where is the pound sign coming from? $_POST? Did you clear your cache? Is the server enforcing UTF-8?
-
You're working in UTF-8: http://www.phpfreaks.com/forums/index.php/topic,163936.0.html What's in your meta tag?
-
The encoding of which text? See iconv.
-
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>
-
http://www.phpfreaks.com/forums/index.php/topic,137431.msg585346.html#msg585346
-
[SOLVED] preg_match, preg_match_all and preg_grep
effigy replied to diamondnular's topic in Regex Help
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. -
There's a discussion on Perl here.
-
[SOLVED] preg_match, preg_match_all and preg_grep
effigy replied to diamondnular's topic in Regex Help
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. -
What metacharacters? echo preg_replace('/[^\da-z.]/', '', $string);
-
What about hyphenated last names, suffixes, or two-word first names?