Jump to content

[SOLVED] Regex picking out a string


jk11uk

Recommended Posts

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  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

[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

Link to comment
Share on other sites

:( :( :(

 

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) ??

Link to comment
Share on other sites

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  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.