SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: regex question

  1. #1
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex question

    hi

    I want to create a custom code and read the attributes.

    [img src="img.png" width="11" alt="adsfafsd"]

    how would i create a regex that recognizes it and gives me the attributes in an array?

    Thanks in advance.

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Do you mean with attribute names as keys?
    PHP Code:
    $result['src']= "img.png";
    $result['width'] = "11"// string or integer or not bothered?
    $result['alt'] = "adsfafsd"

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Shame you are not starting with an xml-friendly string (eg xHTML):
    PHP Code:
    $input '<img src="img.png" width="11" alt="adsfafsd" />';
    $out simplexml_load_string($input);
    var_dump($out);

    // gives
    object(SimpleXMLElement)[1]
      public 
    '@attributes' => 
        array
          
    'src' => string 'img.png' (length=7)
          
    'width' => string '11' (length=2)
          
    'alt' => string 'adsfafsd' (length=8

  4. #4
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks so far.

    I can't use the xml codes i did look into it but there are constraints ;(

    yeah the second post about the results is spot on. i'm not worried about datatypes i can manage that after it's in the array.
    the neigbours (free) WIFI makes it just a little more fun

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you try the following code?

    PHP Code:
    $s '[img src="img.png" width="11" alt="adsfafsd"] test [img src="img.jpg" width="22" alt="hi"]';
    $pattern '#\[img([^\]]+)\]#si';
    if (
    preg_match_all($pattern$s$m))
    {
            
    $pattern '#(\w+="[^"]+")#i';
            
    $attributes = array();
            foreach(
    $m[1] as $m1)
            {
                    if (
    preg_match_all($pattern$m1$attrm))
                    {
                            foreach(
    $attrm as $v)
                            {
                                    
    $tmp = array();
                                    foreach(
    $v as $vv)
                                    {
                                            
    $t explode("="$vv);
                                            
    $tmp[trim($t[0])] = str_replace('"'''$t[1]);
                                    }
                            }
                            
    $attributes[] = $tmp;
                    }
            }
            
    print_r($attributes);


  6. #6
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    although it looks good.. it's not working.
    the neigbours (free) WIFI makes it just a little more fun

  7. #7
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting closer

    hi .. i've changed some of this.

    and the first step is working the problem is that the second pattern isn't picking anything up. i want to find the different attributes. in now is 1 string with all the attributes.

    Code:
    $pattern = '#\\[img([^\]]+)\]#smi';
        if (preg_match_all($pattern, $content, $m))
        {
            $pattern = '#(\w+\="[^\"]+\")#mi';
            $pattern = '#\"(?:[^\\"]+|\\.)*#mi';
            $attributes = array();
            
            foreach($m[1] as $m1)
            {
                    if (preg_match_all($pattern, $m1, $attrm))
                    {
                            // there's nothing here.. 
                        
                            foreach($attrm as $v)
                            {
    
    
                                    $tmp = array();
                                    foreach($v as $vv)
                                    {
                                            $t = explode("=", $vv);
                                            $tmp[trim($t[0])] = str_replace('"', '', $t[1]);
                                    }
                            }
                            $attributes[] = $tmp;
                    }
            }
            print_r($attributes);
    
    
          
            
        }
    the neigbours (free) WIFI makes it just a little more fun

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Does the incoming string contain a single[*] tag, as in your first post, or does it contain multiples as in gvre's example in #5 above?

  9. #9
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you mean there.. the code I just posted is the latest i'm testing with ..

    I marked the point that it's not getting too.
    the neigbours (free) WIFI makes it just a little more fun

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had tested this code before I posted it here.
    It matches one or more [img *] tags. Could you try it again?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •