
effigy
Staff Alumni-
Posts
3,600 -
Joined
-
Last visited
Never
Everything posted by effigy
-
[SOLVED] preg_match_all returns only one match
effigy replied to propellerhead's topic in Regex Help
Regular expressions are not the best tool for nested data, but this works: <pre> <?php $data = <<<DATA <custom> probe1 <custom> probe2 </custom> probe3 </custom> <custom> probe4 </custom> <custom> probe5 </custom> DATA; preg_match_all('%<custom>(??!<custom>)(?!</custom>).)*<\/custom>%s', $data, $matches); print_r($matches); ?> </pre> -
$sql = preg_replace("/'(\d+)'/", '$1', $sql);
-
You have a laziness/greediness issue.
-
?! means does not follow. ?= means follows. ?!=, what you have, means does not follow, then a literal =.
-
What encoding are you using?
-
http://www.regular-expressions.info/lookaround.html
-
Why are you using this and not a character class? This kind of alternation is not optimal.
-
I think it's best to include the + since it doesn't hurt. If the data is coming from a user they could have a typo—"02/27//2009" for instance—which, when split, would create 4 elements rather than 3. However, if there are any delimiters on the ends, this would still create empty elements. I recommend: preg_split('/\D+/', $string, -1, PREG_SPLIT_NO_EMPTY)
-
You can split on \D+.
-
file_get_contents should be fine. Most editors do; I use Notepad++.
-
<pre> <?php $tests = array( 'BBB', 'BBB; BBB', 'BBB; BBB; BBB; BBB', 'B;', ';B', ';;', ); foreach ($tests as $test) { echo "$test => "; echo preg_match('/\A([^;]+;\s*)*[^;]+\z/', $test) ? 'OK' : 'Not OK' ; echo '<br>'; } ?> </pre>
-
<pre> <?php $data = <<<DATA HOW TO WATER ROSES Start by turning the water on.... HOW TO MOW Start the mower... DATA; print_r(preg_split('/(?:\r?\n)+/', $data)); ?> </pre>
-
You need to use the /m modifier if you want $ to match around all new lines, rather than just the end of the string.
-
What's the value of $unicode? This gives me "réréré": <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <pre> <?php $text = 'r%C3%A9r%C3%A9r%C3%A9'; echo urldecode($text); ?> </pre>
-
"Suddenly"? Perhaps your input and/or code changed? Have you examined the data fully? A hex dump, perhaps?
-
You've bad Unicode. See "Replacement character" here.
-
Your first .*? will capture anything up to prodID, even it has to go beyond another double quote or into another tag. Try |href="([^"]+prodID=[^"]+)"[^>]*>(.*?)</a>|.
-
Are all of these Kanji? You can use their code point ranges to match them, e.g. preg_match_all('/[\x{3000}-\x{303F}]+/u', $string, $matches);.
-
[SOLVED] Storing "Non-English" characters under MySQL
effigy replied to paragkalra's topic in MySQL Help
http://www.phpfreaks.com/forums/index.php/topic,190618.msg855845.html#msg855845 -
Turn your warnings on: Warning: preg_match_all() expects parameter 2 to be string, array given. You need to examine the results; I recommend print_r($byname);
-
Even better, be specific: '/href="([^"]*)"/i' The double quotes were escaped in the regex because they were used to delimit the string; they will match an unescaped quote: <pre> <?php $str = '"'; echo preg_match("/\"/", $str); ?> </pre>
-
Apparently it requires the delimiter: preg_match('/' . preg_quote($start, '/') . '(.*?)' . preg_quote($end, '/') . '/', $data, $matches);
-
/(\b(?!(?:else)?if)\S+\b\s*\()/