jk11uk Posted February 1, 2008 Share Posted February 1, 2008 Hi i have made a script that goes through my site, reads in page code and then finds a specific value. If looks for a certain string that is always before the value i need to extract. And it also looks for a string that is always after the value i extract (then returns whats in the middle, my value). However, if this value is over 1000 there is a comma (1,000) and my code doesn't like this. Can anyone suggest a different way of doing this so that the value can contain punctuation? Maybe using regex? thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/ Share on other sites More sharing options...
sasa Posted February 1, 2008 Share Posted February 1, 2008 remove ',' after grab value Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455120 Share on other sites More sharing options...
jk11uk Posted February 1, 2008 Author Share Posted February 1, 2008 the function i'm using to get the code is: function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } $code = file_get_contents("http://www.site.com"); list($words) = textbetweenarray("word one word two ", " end word", $code); echo $words; Really need some genious here, this is way over my head! thanks again Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455218 Share on other sites More sharing options...
laffin Posted February 1, 2008 Share Posted February 1, 2008 [code]<?php function textbetweenarray($s1,$s2,$s) { $matches=array(); preg_match_all("@$s1(.*)$s2@imsU",$s,$matches); return $matches[1]; } $oldcode= ' function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } '; header("Content-type: text/plain"); $matches=textbetweenarray('\(','\)',$oldcode); print_r($matches); ?> Array ( [0] => $s1,$s2,$s [1] => [2] => $s1 [3] => $s2 [4] => $s1 [5] => $s2 [6] => $s [7] => $scheck,$s1 [8] => $pos1!==false [9] => substr($scheck,$pos1+$L1 [10] => $pos2!==false [11] => $s,$pos1+$L1,$pos2 [12] => $s,$pos1+$L1+$pos2+$L2 [13] => $s [14] => ($pos1!==false [15] => $pos2!==false )[/code] now it dun work for nested stuff. if ya needs nested stuff. than it gets a lot more complicated Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455237 Share on other sites More sharing options...
sasa Posted February 1, 2008 Share Posted February 1, 2008 change line $myarray[]=substr($s,$pos1+$L1,$pos2); to $myarray[]= str_replace(',', '', substr($s,$pos1+$L1,$pos2)); Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455258 Share on other sites More sharing options...
resago Posted February 1, 2008 Share Posted February 1, 2008 $value=preg_replace('/.*before(.*)after.*/is',"$1",$source); $source can be a string. might work if $source is an array, $value would then be an array. Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455283 Share on other sites More sharing options...
jk11uk Posted February 1, 2008 Author Share Posted February 1, 2008 :( unfortunately none of these suggestions work. looks like a tough one the problem is (i think) that the function im using never deals with the value it is extracting until it is output. So there is no way of replaceing punctuation with a space etc until it is too late. by which time the punctuation has cause the function to fail. So does any php-pro know a way to find this value with a different function? so, taking a page of code and finding a changing value inbetween two other unchanging values (can always count on them being the same) ?? Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455474 Share on other sites More sharing options...
sasa Posted February 1, 2008 Share Posted February 1, 2008 can you send some data for test Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455520 Share on other sites More sharing options...
laffin Posted February 1, 2008 Share Posted February 1, 2008 until it's outputted? why cant u insert the routine before/during the output? if that can't be done, consider using ob_start/ob_get_contents/ob_clean functions. which will capture the output into a string, than ya can process and echo it out. Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-455673 Share on other sites More sharing options...
jk11uk Posted February 4, 2008 Author Share Posted February 4, 2008 no worries, i got it working with another method. probably a very long way round but hey it works. i basically said get everything in the code before a certain string (and put into var X), then get everything after a certain string (put into var Y). then replace everything in the code matching X with ' ' and make that var Z. then replace everythin in var Z matching Y with ' '. then minus the first string from the last output. thanks a lot anyway guys Quote Link to comment https://forums.phpfreaks.com/topic/88860-solved-regex-picking-out-a-string/#findComment-457427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.