1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

preg_match_all array on a string

Discussion in 'PHP' started by anilinkz, Aug 7, 2010.

  1. #1
    i have a string

    $content = "I Love Digital Point so Much";
    PHP:
    and want to do a match so i can get the "Digital", "Point" and the "Much" on the output.


    can i do an array on the pattern part of preg_match_all

    preg_match_all(array("/Digital/","/Point/","/Much/"), $content, $match);
    PHP:
    so i wont have to use preg match all always.

    since im using the code to find the different and many patterns found on a single string and so the output can be something like

    $match[1][0] outputs Digital
    $match[1][1] outputs Point
    $match[1][2] outputs Much

    TIA. need it for finals. T_T
    SEMrush
     
    anilinkz, Aug 7, 2010 IP
    ThePHPMaster likes this.
    SEMrush
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    If its a case of you have a list of unwanted words "I, love or so" you could do something like this: preg_replace()

    
    <?php
    
    $content = 'I Love Digital Point so Much';
    $patterns = array();
    $patterns[0] = '/I/';
    $patterns[1] = '/Love/';
    $patterns[2] = '/so/';
    $replacements = array();
    $replacements[2] = '';
    $replacements[1] = '';
    $replacements[0] = '';
    echo preg_replace($patterns, $replacements, $content);
    
    ?>
    
    PHP:
    Which will replace those words with nothing thus the output is as requested: Digital Point Much

    Or maybe there's some function() you have to use, I assumed from your post you didn't want to you preg_match_all()
     
    MyVodaFone, Aug 7, 2010 IP
    anilinkz likes this.
  3. anilinkz

    anilinkz Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i need to MATCH "Digital", "Point" and "Much" from the string so i can use the matched strings in some other things. so replace is not an option.
     
    anilinkz, Aug 7, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    OK how about this... basically we give our search pattern terms a name like <digital> and if we get a match its $matches['digital'] you can of course number these instead, also we are searching for both upper and lower case, just in case they try and catch you out. :)
    
    <?php
    
    $content = 'I Love Digital Point so Much';
    
    $pattern = '#(?<digital>Digital|digital)(.*)(?<point>Point|point)(.*)(?<much>Much|much)#';
    
    preg_match($pattern, $content, $matches);
    
    $digital = $matches['digital'];
    $point =  $matches['point'];
    $much = $matches['much'];
    
    echo $digital;
    echo "<br />";
    echo $point;
    echo "<br />";
    echo $much;
    
    ?>
    
    PHP:
     
    Last edited: Aug 7, 2010
    MyVodaFone, Aug 7, 2010 IP
  5. anilinkz

    anilinkz Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    is it possible to do a regex for all the patterns? since i'm not just looking for a word but also regex...

    and from what i understand in the code, it's not possible to interchange the pattern....


    let's say the string
    $content = "I Love Digital Point so Much very very Much digital Love";
    PHP:
    so i need to get all the "Digital", "Point" and "Much".

    i can't just use this....

    $pattern = '#(?<digital>Digital|digital)(.*)(?<point>Point|point)(.*)(?<much>Much|much)(.*)(?<much>Much|much)(.*)(?<much2>Much|much)(.*)(?<digital2>Digital|digital)#';
    PHP:
    it would be a pain. XD
     
    anilinkz, Aug 7, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    No your right, the code I provided means the words must come in the order Digital, Point and Much, I'm sorry but the scoop of your requirements is beyond my general knowledge, maybe someone else will participate in the thread.

    At a glance its going to take a lot of time for someone to code this for you, you could try the programming section here on DP or if your interested in a recommendation try a PM to @danx10 for a quote on the job.
     
    Last edited: Aug 8, 2010
    MyVodaFone, Aug 8, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    <?php
    
    $content = 'I Love Digital Point so Much';
    
    preg_match_all('~\b([A-Z][a-z]+)\b~', $content, $a);
    
    echo '<pre>';
    print_r($a);
    echo '</pre>';
    ?>
    PHP:
     
    danx10, Aug 8, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    I think he wants to take all the matching words "digital point and much" from a random string ie:
    $content = "I Love Digital Point so Much very very Much digital Love"; 
    PHP:
    ..and assign those individually to a variable for future use within his script.

    So from your code output how does he check against the array results to get the corresponding word match result ?

    Would he need to do a lot of if statements == digital ?

    
    Array
    (
        [0] => Array
            (
                [0] => Love
                [1] => Digital
                [2] => Point
                [3] => Much
            )
    
        [1] => Array
            (
                [0] => Love
                [1] => Digital
                [2] => Point
                [3] => Much
            )
    
    )
    
    
    Code (markup):
     
    MyVodaFone, Aug 8, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Thats what the code does:

    $a is an array, theirfore you can pick/select/output specic values like so:

    <?php
    
    $content = 'I Love Digital Point so Much';
    
    preg_match_all('~\b([A-Z][a-z]+)\b~', $content, $a);
    
    /* would output Love */
    echo $a[0][0];
    
    /* would output Digital */
    echo $a[0][1];
    
    //etc..
    ?>
    PHP:
    I believe all the OP wants is the be able to output them (not validate/compare theirfore no need to use if statements)
     
    danx10, Aug 8, 2010 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    Yeah, that was his first statement until he said he wanted all instances ie its going to be a random $string so we don't know what echo $a[0][0]; is going to be, maybe I am misunderstanding what the op wants.

    However here's something that will out-put the first 5 instances of either word, hopefully 5 will be enough?

    each instance is assigned like $digital[0][0], $point[0][2] etc.. as to how you find out if you have an instance or not, I have no idea.

    
    <?php
    
    $content = "I Much Digital Love much digital point point Digital love Point so Much point very love point very Much digital Love";
    
    
    function get_digital($regex,$content)
    {
    	preg_match_all($regex,$content,$Digital);
        return $Digital[0][0] . '  ' . $Digital[0][1] . '  ' . $Digital[0][2] . '  ' . $Digital[0][3] . '  ' . $Digital[0][4];
    }
    
    function get_point($regex,$content)
    {
    	preg_match_all($regex,$content,$Point);
        return $Point[0][0] . '  ' . $Point[0][1] . '  ' . $Point[0][2] . '  ' . $Point[0][3] . '  ' . $Point[0][4];
    }
    
    function get_much($regex,$content)
    {
    	preg_match_all($regex,$content,$Much);
        return $Much[0][0] . '  ' . $Much[0][1] . '  ' . $Much[0][2] . '  ' . $Much[0][3] . '  ' . $Much[0][4];
    }
    
    $digital = get_digital('#(Digital|digital)#',$content);
    $point = get_point('#(Point|point)#',$content);
    $much = get_much('#(Much|much)#',$content);
    
    echo $digital;
    echo "<br />";
    echo $point;
    echo "<br />";
    echo $much;
    ?>
    
    PHP:
    PS: please remember I'm a newbie so no laughing:eek:
     
    Last edited: Aug 8, 2010
    MyVodaFone, Aug 8, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    The OP meant the $content will be a random string, but the concept will be the same no matter how the format of the random string is...if it follows the pattern of the regex (as its generic) it will match and the matches will be contained within the array.

    In that array the OP can loop through to display its contents/values...or print_r() to see specific values and then do whatever the OP'd like with them.

    Furthermore:

    $digital = get_digital('#(Digital|digital)#',$content);
    PHP:
    This can be achieved in a simpler form:

    $digital = get_digital('#(digital)#i',$content);
    PHP:
    By add the i modifier to the end of the expression, which will make it case insensitive, theirfore wouldn't matter what case :)
     
    Last edited: Aug 8, 2010
    danx10, Aug 8, 2010 IP
    anilinkz likes this.
  12. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    This could be the function you're after, but doesn't use regular expressions. Useful to extract parts of a string from a string.

    function return_strings_between($text, $openingMarker, $closingMarker) {
        $openingMarkerLength = strlen($openingMarker);
        $closingMarkerLength = strlen($closingMarker);
    
        $result = array();
        $position = 0;
        while (($position = strpos($text, $openingMarker, $position)) !== false) {
          $position += $openingMarkerLength;
          if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
            $result[] = substr($text, $position, $closingMarkerPosition - $position);
            $position = $closingMarkerPosition + $closingMarkerLength;
          }
        }
        return $result;
      }
    PHP:
     
    caciocode, Aug 8, 2010 IP
    anilinkz likes this.
  13. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #13
    This should work:

    
    $content = "I Love Digital Point so Much very very Much digital Love";
    $wordList = array('digital','point','much');
    
    foreach($wordList as $word)
    {
    	preg_match_all('/.*('.$word.').*/Ui',$content,$result);
    	print_r($result[1]);
    }
    
    PHP:
    A bit long, but that will work no matter how many number of digital or point there are.
     
    ThePHPMaster, Aug 8, 2010 IP
    anilinkz likes this.