In some code I;m writing, I have the following bit:

preg_match_all('value="^[0-9]+-[0-9]+$',$wTxt,$LocationMatches)

$wTxt is a bit of HTML code that should contain 3, 5, or 8 matches to the above expresion, creating $LocationMatches as an array with 3,5, or 8 entries. In this case, the entries are x-y co-ordinates. (If the code I'm using above won't do that, let me know, of course!)

The sticking point for me is, I want to find the average value of x and the average value of y. I'm betting a proper use of some math and the foreach iteration on $LocationMatches would do the trick, but can't figure out the method to using the keys combined with the iteration and such.

Here's an example of a rather likely array I'd expect to see as a result of the above code and need worked on and the results I need to get:
example of likely input: $LocationMatches = array('22-10',23-10','24-10','22-11','24-11','22-13',23-13','24-13')
desired output from example $LocationMatches array: $xPos=23 $yPos=11

note- decimal result values for $xPos and $yPos are OK and are expected in certain cases.

    <?php
    $wTxt = 'test value="22-10" text value="13-15" text text value="23-14" text';
    preg_match_all('/value="(\d+)-(\d+)"/', $wTxt,$LocationMatches, PREG_SET_ORDER);
    $xSum = 0;
    $ySum = 0;
    foreach($LocationMatches as $location)
    {
       $xSum += $location[1];
       $ySum += $location[2];
    }
    $xAvg = $xSum / count($LocationMatches);
    $yAvg = $ySum / count($LocationMatches);
    echo "<p>Text: '$wTxt'</p>\n";
    echo "<p>Average x: $xAvg, Average y: $yAvg</p>\n";
    ?>
    

    Output:

    Text: 'test value="22-10" text value="13-15" text text value="23-14" text'

    Average x: 19.3333333333, Average y: 13

      The output I get when I open the above code is as follows:

      Text: '$wTxt'

      \n"; echo "

      Average x: $xAvg, Average y: $yAvg
      \n"; ?>

      Is there maybe something wrong with my server / php installation?

        I got the same thing when I removed the opening PHP tag.

        NogDog's code works just fine here.

          Hmm- nope, I didn't leave off the initial <?php. I've made that mistake before, and learned not to do it again.

          Its not a deal now because I modified the above code to work in the place where I actually need it to. The actual variable I was using instead of $wTxt is a $_POST[] array member, and it seems to come with a \ inserted before every ", which was messing up the regex he used. I dumbed the regex down a bit (not a problem with the strings I should be getting) and it works great now.

          Thanks again!

          Edit- as a follow up, I must say, I'm confused by the pattern used in the above code. Based on the refrences I can find, I figured it would have a ^ at the start of the matched section, and a $ at the end, but when I use those I get errors like "No ending delimiter '' found".

          Another instance I had trouble with was a fairly simple problem, but I couldn't find the pattern to do it based on any refrences I had.

          preg_match_all('^<b>\w+</b>$',$_POST['wScene'],$CharNameMatch);
          $CharName = $CharNameMatch[0][0];
          $CharName = substr($CharName,3,-4);

          What I WANT the above code to do is set $CharName to be the text (only) of the first piece of bolded text in an arbitrary block of html style code. I knew that there is something like <b>Name</b> in the stuff.

          Looking at NogDog's code, I came up with the following, which works:

          preg_match_all('/<b>\w+<\/b>/',$_POST['wScene'],$CharNameMatch);
          $CharName = $CharNameMatch[0][0];
          $CharName = substr($CharName,3,-4);

          However, I have no idea WHY "/<b>\w+<\/b>/" is the pattern I should use. Well, I know the interiro stuff, but I don't see why there is a "/" at the start and end of it.

            When using the preg_*() functions, the regular expression must start and end with a delimiter (because regex modifiers may follow the closing delimiter). The "traditional" delimiter is the slash, but many other special characters can be used, as long as (1) the same delimiter is used at the start and end of the regex, and (2) if that character appears within the regex itself then it must be escaped by a back-slassh.

            The "" and "$" characters in a regex (when not use within a character class) represent the beginning and end, respectively, of the string being searched. So they "anchor" the regex to that end of the string being searched, effectively saying that that end of the regex must either start at the very beginning of the string or end at the very end of the string. It therefore makes no sense to use either in the middle of a regex.

            // true if "something" appears at very start of string:
            preg_match('/^something/', $string)
            
            // true if "something" appears at very end of string:
            preg_match('/something$/', $string)
            
            // true if "something" is the entire string:
            preg_match('/^something$/', $string)
            

              Huh, that makes it as clear as cola now, instead of mud. 🙂

              I expect the project I'm doing will be using a fair amount of preg_*() functions and regex, as it ultimatley amounts to pulling info out of html files and putting it into a database. Is there any online reference / tutorial yu could recommend for this stuff? I assume at some point I will want / need to get Mastering Regular Expressions
              By Jeffrey E. F. Friedl, but for now I might need to save some cash.

                Write a Reply...