Jump to content

ddrudik

Members
  • Posts

    78
  • Joined

  • Last visited

Everything posted by ddrudik

  1. function repfunc($match){ // do something with $match[1] // return something effectively replacing $match[0] } $out=preg_replace_callback('/\{(\$[^}]*)\}/','repfunc',$in);
  2. I will just assume that I have to test all alternatives to get the actual speed results for a given match pattern and source string.
  3. In that code snippet I don't see $time_end defined, so your values would have no reference ending point, as well we can't round to 4 places since our values are 1 place smaller than that. Consider this code: <?php $time_start = microtime(true); $str='www.google.com.sports'; if(preg_match('~^((?:[^.]+\.){2})(.*)~',$str,$parts)){ // NRG $str=$parts[1].str_replace('.','/',$parts[2]); } echo $str; $time_end = microtime(true); $elapsed_time = $time_end-$time_start; echo $elapsed_time . '<br />'; $time_start = microtime(true); $str='www.google.com.sports'; if(preg_match('~^((?:.*?\.){2})(.*)~',$str,$parts)){ // ddrudik $str=$parts[1].str_replace('.','/',$parts[2]); } echo $str; $time_end = microtime(true); $elapsed_time = $time_end-$time_start; echo $elapsed_time . '<br />'; ?> This result: www.google.com/sports4.1007995605469E-5 www.google.com/sports1.3828277587891E-5 The speed of the previous thread's solution must have been influenced by different factors, maybe it's the use of capture groups in this example, not sure.
  4. My benchmark testing might be flawed, but that pattern runs slower for me: <?php $time_start = microtime(true); $str='www.google.com.sports'; if(preg_match('~^((?:[^.]+\.){2})(.*)~',$str,$parts)){ $str=$parts[1].str_replace('.','/',$parts[2]); } echo $str; echo "<br>".(microtime(true)-$time_start)."<hr>"; $time_start = microtime(true); $str = 'www.google.com.news'; $arr = explode('.', $str); $total = count($arr); if($total > 3){ $newArr = $arr[0] . '.' . $arr[1] . '.' . $arr[2]; for($i = 3; $i < $total; $i++){ $newArr .= '/' . $arr[$i]; } echo $newArr; } echo "<br>".(microtime(true)-$time_start)."<hr>"; $time_start = microtime(true); $str='www.google.com.sports'; if(preg_match('~^((?:.*?\.){2})(.*)~',$str,$parts)){ $str=$parts[1].str_replace('.','/',$parts[2]); } echo $str; echo "<br>".(microtime(true)-$time_start)."<hr>"; ?> Output: www.google.com/sports 3.9815902709961E-5 -------------------------------------------------------------------------------- www.google.com/news 1.7881393432617E-5 -------------------------------------------------------------------------------- www.google.com/sports 1.2874603271484E-5
  5. There seems to be a (very slight) speed advantage to using this method instead: $str='www.google.com.sports'; if(preg_match('~^((?:.*?\.){2})(.*)~',$str,$parts)){ $str=$parts[1].str_replace('.','/',$parts[2]); } echo $str;
  6. I am usually answering questions in a non-platform-specific regex forum, looking back I guess I knew you were using PHP. Sorry about the question. nrg_alpha had the answer.
  7. This would seem to require some code, what platform are you using? (C#.NET,PHP, etc.)
  8. It would seem the recommendation to test with the font's widest character is a valid one. Otherwise, measure the pixel width of each character, store that in an array, then use array_count_values on preg_match_all output and add up the pixel widths of the character counts found (seems more work than necessary though).
  9. This isn't necessarily a cut-and-paste solution, but it should give you an idea of how to go about finding out the values and adjustments etc. You should consider finding out what maximum number of characters in a name can be printed successfully, then adjust the font size as needed based on the length of the name if it exceeds that number. <?php $fontsize=10; $maxprintable=6; $name='This Is A Long Name.'; $namelength=strlen($name); if($namelength>$maxprintable){ $fontsize=($maxprintable/$namelength)*$fontsize; } echo $fontsize; ?>
  10. function checkval($letters,$val){ return preg_match('/(?=^['.preg_quote($letters,'/').']{5,}$)(?!.*(.).*\1)/',$val) ? "pass" : "fail"; } echo checkval('ygabodfe','yodeag'); echo '<br>'.checkval('ygabodfe','yadea'); echo '<br>'.checkval('ygabodfe','yodeq');
  11. While testing with various sites I noticed that some sites such as msn.com had img src URLs that referenced an image with a # sign and then more (unwanted) data, so I modified the expression to drop the # sign and anything after that if encountered (as it does with the ?): http://pastebin.com/f1935e1d6
  12. My code in my original post was munged by the forum server during posting (\'s removed before alpha characters and a ' removed from within [ ] character set and before the pattern, quite unintended changes that makes me concerned about posting future source code here). Here's the proper code posting: http://pastebin.com/f42a371cc Here's the pattern in action matching against img tags without quotes, with single quotes, and with double quotes: http://www.myregextester.com/?r=020906eb The array_filter and array_merge in my code is done to merge the results of quoted and non-quoted images into one array result if they happen to appear on the same page, you can see the results of the preg_match_all code before merging in the example link above.
  13. This is a starter function, you could certainly extend it by checking for relative URLs in the returned array and then prepending the directory URL of the page so that the image src would be complete etc. <pre> <?php function getimgs($page){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $page); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); preg_match_all('/<img\s[^>]*\bsrc=(??:([\'"])((??!\1|\?).)*))|([^\s?>]*))/is',curl_exec($ch),$images); curl_close($ch); $images=array_merge(array_filter($images[2]),array_filter($images[3])); return $images; } echo print_r(getimgs('http://www.phpfreaks.com/forums/'),true); ?>
  14. How specific is your search and replace? Is it just replacing the text between tags with empty string? Please explain.
  15. My 'reverse' function arrived at the same result with the included primes array, albeit a bit slower. after 5015 iterations, the sum of diagonals for 2007: 87676673138 elapsed time: 1.449 seconds
  16. BTW, for the curious, with the include file and replacing '$primes = generate_primes($n*$n);' with 'global $primes;' in the fine PHP code by GingerRobot (time shown using microtime function): Sum of diagonals for n=2007: 87676673138 Time taken: 0.002 seconds
  17. That's a much more efficient function than mine, I'll admit I stumbled across the question while waiting to answer regex questions elsewhere, I just recognized a pattern in the prime array for diagonals so I took a shot. In any case it was an interesting question and rather interesting to see that I started from the outside of the spiral and worked in while others started from the inside out. If anyone's interested in the 33.8MB file with the 2007*2007 primes array text file, here it is: http://www.4shared.com/file/69220338/12b559e2/primesphp.html I created it with this C#.NET code converted from the PHP post by GingerRobot (I understand that it could all be done in C#.NET faster than PHP but this really isn't an exercise in performance). using System; using System.Collections; using System.Text; using System.IO; namespace generateprimes { class Program { static void Main(string[] args) { genprimes(2007); } public static void genprimes(double num) { num = Math.Pow(num, 2); int generated = 1; int x = 3; FileStream fs = new FileStream("primes.txt", FileMode.Create, FileAccess.Write, FileShare.None); StreamWriter swFromFileStream = new StreamWriter(fs); swFromFileStream.Write("<?php\r\n$primes=array(2,"); for(x=3;;x+=2) { bool prime = true; int y = 0; for (y = 2; y <= Math.Sqrt(x); y++) { if (x % y == 0) { prime = false; break; } } if (prime == true) { generated++; Console.WriteLine(generated+"..."+x); swFromFileStream.Write(x+","); if (generated >= num) { break; } } } swFromFileStream.Write(");\r\n?>"); swFromFileStream.Flush(); swFromFileStream.Close(); } } } It wasn't usable for me as an include until I disabled the memory_limit in PHP: <?php ini_set("memory_limit","-1"); include 'primes.php'; echo count($primes); ?> Note that rn above in my C#.NET code should be \r\n that is being munged by the post.
  18. GingerRobot, I appreciate that vote of confidence in my code, I will wait for the C#.NET code to complete the task.
  19. I converted that code to C#.NET, I will let you know the result of 2007 if/when it completes processing on my test server. using System; using System.Collections; using System.Text; namespace sumdiagprimes { class Program { static void Main(string[] args) { Console.WriteLine(sumdiagprimes(3)); Console.WriteLine(sumdiagprimes(4)); Console.WriteLine(sumdiagprimes(5)); Console.WriteLine(sumdiagprimes(75)); Console.WriteLine(sumdiagprimes(2007)); } public static string sumdiagprimes(int size) { long startTime = DateTime.Now.Ticks; int j = 2; ArrayList primes = new ArrayList(); while (primes.Count < Math.Pow(size, 2)) { if (isprime(j)) { primes.Add(j); } j++; } primes.Reverse(); int p = size - 1; int q = 0; int iter = 0; ArrayList result = new ArrayList(); while(q<=primes.Count-1 && p>0) { for(int i=1; i<=5; i++) { iter++; if (q <= primes.Count - 1) { //Console.WriteLine("p="+p+" q="+q); if (result.Count==0 || (result[result.Count - 1] != primes[q])) { result.Add(primes[q]); } q = q + p; } else { break; } } //Console.WriteLine("old q=" + q); q = q - p; //Console.WriteLine("new q="+q); p=p-2; } if((size % 2) != 0) { result.Add(result[result.Count-1]); } Int64 resultsum = 0; for (int i = 0; i <= result.Count-1; i++) { resultsum = resultsum + Convert.ToInt64(result[i]); } //PrintValues(result); long endTime = DateTime.Now.Ticks; TimeSpan timeTaken = new TimeSpan(endTime - startTime); return "size: "+size+"\r\niter: "+iter+"\r\ntime: "+timeTaken.ToString()+"\r\n sum: "+resultsum+"\r\n\r\n"; } public static bool isprime(int number) { int i = 0; if (number < 2) { return false; } else { for (i = 2; i <= (number / 2); i++) { if (number % i == 0) { return false; } } return true; } } public static void PrintValues(IEnumerable myList) { foreach (Object obj in myList) Console.Write("{0},", obj); Console.WriteLine(); } } }
  20. Maybe someone can fix the performance issue with the code to allow it to return the result for 2007 (the function times out for me after somewhere after 75): <pre> <?php function check_prime($num) { if($num == 2 || $num == 3) return true; if($num > 4 && $num & 0x01) { $sq = sqrt($num); if(floor($sq) != $sq) { for($a = 5; $a <= ($num >> 1); $a += 2) if($num % $a == 0) return false; return true; } } return false; } function sumdiagprimes($n){ $r=0;$c=0;$i=2;$p=0;$q=0;$iter=0; while($c<pow($n,2)) { if(check_prime($i)){ $c++; $primes[$c]=$i; $r=$r+$i; } $i++; } $primes=array_reverse($primes); $p=$n-1; while(isset($primes[$q]) && $p>0){ for($i=1; $i<=5; $i++){ $iter++; if(isset($primes[$q])){ $result[$q]=$primes[$q]; $q=$q+$p; } else { break; } } $q=$q-$p; $p=$p-2; } if(($n/2)!=intval($n/2)){ $result[$q+1]=$result[$q]; } return "after $iter iterations, the sum of diagonals for $n: ".array_sum($result)."\r\n"; } echo sumdiagprimes(3); echo sumdiagprimes(4); echo sumdiagprimes(5); echo sumdiagprimes(75); //echo sumdiagprimes(2007); ?> Returns: after 5 iterations, the sum of diagonals for 3: 60 after 10 iterations, the sum of diagonals for 4: 157 after 10 iterations, the sum of diagonals for 5: 330 after 185 iterations, the sum of diagonals for 75: 2588028
  21. If you preg_quoted $matcharray2 in that manner and it contained /something/ it would become \/something\/ thus your error.
  22. Also, if you want to exclude search_term from within head/script/a blocks as well as from within tags: $html=preg_replace_callback('~(<head>.*?</head>|<script\s[^>]*>.*?</script>|<a\s[^>]*>.*?</a>)|search_term(?!(?=[^<>]*>))~is',create_function('$matches','return isset($matches[1]) ? $matches[1] : "<strong>$matches[0]</strong>" ;'),$html);
  23. this should cover the wildcards * and ?: <?php echo boldWords(array("o?e", "a**", "t??", "b*e"), "One is before two and three is too."); function boldWords($words, $string) { $regex = "~\b(?:".preg_replace('/(?:\\\\\*)+/','.*?',str_replace('\?','.',implode('|', array_map('preg_quote',$words)))).")\b~i"; return preg_replace($regex ,'<strong>$0</strong>',$string); } ?>
  24. You might want to consider a pre-made syntax highlighter for PHP (many are available), the issue with any regex pattern for this operation would be how do you keep from matching your target strings within string declarations etc. /\*(??!\*/).)*\*/|(?://|#)[^\r]*
×
×
  • 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.