Announcement

Collapse
No announcement yet.

the regex problem again..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • the regex problem again..

    well...

    my objective is like this... i'm having my own code and not html tags.

    so, i don't use < center > but [align="center"]


    now,..

    i wanted to make a function named [img]

    it will look like this:

    [img thumb="path_to/thumb.gif" pic="path_to/full_size_pic.gif"]

    i just want to know, how to make that, if i have thumbnail i will write thumb="...", but if i dont have it, i won't enter the thumb="".

    if i have thumbnail:
    [img thumb="path_to/thumb.gif" pic="path_to/full_size_pic.gif"]

    if i don't have thumbnail:
    [img pic="path_to/full_size_pic.gif"]


    is it possible to make the regex like that?

    if you don't know what i mean,.. please tell me a good tutorial where can i learn how the regex works. i actually see after 1 year of php coding, how much of regex do i need...

    thank you all

  • #2
    I'm completely lost. What do you want to pattern match with regex?

    Comment


    • #3
      Regular Expressions!

      if i tell you what should preg_replace (or any other similar function) do:

      1. it searches the variable $text for the [img...] part.
      2. then it should depend if you entered the thumb="" part or not


      that's all what i'm searching for...

      i know i must use the chars like: ^ and | and () etc etc... but don't know how and where.

      __________________________

      the preg_replace searches for the [img...] part, and if this part has thumbnail="somepic.gif" in it call function: showimg (thumb_yes). if it doesn't have that part in it: call function showimg (thumb_no)..

      i hope, what i mean..


      if you still don't know, please give me some good tutorial for newbies on how to handle with Regular Expressions.

      thanks

      Comment


      • #4
        I love regex...
        Took me a while, but I think I've got it packed as small as I can....
        Code:
        if (preg_match('#\[img\s+(thumb=(["\']?)([^"\']+)\\2)?\s*(pic=(["\']?)([^"\']+)\\5)?\s*(thumb=(["\']?)([^"\']+)\\8)?\]#i', $string, $match)) {
            foreach ($match as $key=>$val) {
                echo "$key = $val\n";
            }
        }
        else {
            echo "no luck\n";
        }
        Here are some samples:
        Code:
        string: [img pic="asdf" thumb='test']
        output:
        0 = [img pic="asdf" thumb='test']
        1 = 
        2 = 
        3 = 
        4 = pic="asdf"
        5 = "
        6 = asdf
        7 = thumb='test'
        8 = '
        9 = test
        
        
        string: [img pic="asdf"]
        output:
        0 = [img pic="asdf"]
        1 = 
        2 = 
        3 = 
        4 = pic="asdf"
        5 = "
        6 = asdf
        
        string: [img thumb='test']
        output:
        0 = [img thumb='test']
        1 = thumb='test'
        2 = '
        3 = test
        
        
        string: [img thumb='test' pic="asdf"]
        output:
        0 = [img thumb='test' pic="asdf"]
        1 = thumb='test'
        2 = '
        3 = test
        4 = pic="asdf"
        5 = "
        6 = asdf
        A few input rules...
        The regex is case insensitive, and it doesn't matter if you use single quotes, or double quotes. But if you use a single quote to start the path, you must use the single quote to end it. You may use single for one and a double for the other. This means that thumb='test" is invalid, but both thumb='test' and thumb="test" are valid.

        The output...
        The output will be in an array. The pic="" value will always be number 6. The thumb="" value will be either 3 or 9 (depending if thumb comes before or after pic in the string).

        Comment


        • #5
          BTW, you can learn regex for PHP here:
          LXXXIV. Regular Expression Functions (Perl-Compatible)

          Comment


          • #6
            works fine,..

            if my string has only one [img] code in it..

            what if i have a string like this:


            $string = "
            blah blah blah blah [img pic="somepic.jpg"] blah blah again...


            and offcourse more blah with some cute...

            [img thumb="somethumb.jpg" pic="somepic.jpg"]

            blah!
            ";

            that's my next problem, because the preg_match matches then both img codes..

            any chances to do it right?

            thanks... y00 w00t!

            Comment


            • #7
              I just used preg_match() to show how the regex pattern I made works. You can throw it into any of the preg_ functions.

              What do you want to do with it?
              I'm guessing that you want to print the <img> tag with the thumb, and link it to the pic?

              Quikie Code (untested)
              PHP Code:
              if (preg_match_all('#\[img\s+(thumb=(["']?)([^"']+)\\2)?\s*(pic=(["']?)([^"']+)\\5)?\s*(thumb=(["']?)([^"']+)\\8)?\]#i'$string$match)) {
                  foreach (
              $match as $tmp) {
                      if (
              $tmp[3]!=NULL && $tmp[6]!=NULL) {
                          
              // print thumb with link to pic
                          
              $string str_replace($tmp[0], "<a href='$tmp[6]'><img src='$tmp[3]'></a>"$string);
                      }
                      elseif (
              $tmp[9]!=NULL && $tmp[6]!=NULL) {
                          
              // print thumb with link to pic
                          
              $string str_replace($tmp[0], "<a href='$tmp[6]'><img src='$tmp[9]'></a>"$string);
                      }
                      elseif (
              $tmp[6] != NULL) {
                          
              // just print the pic
                          
              $string str_replace($tmp[0], "<img src='$tmp[6]'>"$string);
                      }
                  }

              Comment


              • #8
                thanks max, but that code doesn't work.

                Code:
                $string = " 
                blah blah blah blah [img pic=\"somepic.jpg\"] blah blah again... 
                
                
                and offcourse more blah with some cute... 
                
                [img thumb=\"somethumb.jpg\" pic=\"somepic.jpg\"] 
                
                blah! 
                "; 
                
                
                if (preg_match_all('#\[img\s+(thumb=(["\']?)([^"\']+)\\2)?\s*(pic=(["\']?)([^"\']+)\\5)?\s*(thumb=(["\']?)([^"\']+)\\8)?\]#i', $string, $match)) {
                    foreach ($match as $tmp) {
                        if ($tmp[3] != NULL && $tmp[6] != NULL) {
                            // print thumb with link to pic
                            $string = str_replace($tmp[0], "<a href='$tmp[6]'><img src='$tmp[3]'></a>", $string);
                        }
                        elseif ($tmp[9] != NULL && $tmp[6] != NULL) {
                            // print thumb with link to pic
                            $string = str_replace($tmp[0], "<a href='$tmp[6]'><img src='$tmp[9]'></a>", $string);
                        }
                        elseif ($tmp[6] != NULL) {
                            // just print the pic
                            $string = str_replace($tmp[0], "<img src='$tmp[6]'>", $string);
                        }
                    }
                }
                
                echo $string;

                doesn't work. please help

                ps:
                I'm guessing that you want to print the <img> tag with the thumb, and link it to the pic?
                -- yes..

                please please help,

                thank you!

                Comment


                • #9
                  tested:
                  Code:
                  <?php
                  
                  $string=<<<myHTML
                  
                  blah
                  [img thumb='thumb1' pic="pic1"]
                  blah
                  [img thumb='thumb2' pic="pic2"]
                  
                  myHTML;
                  
                  echo "<html><body><pre>\n";
                  
                  if (preg_match_all('#\[img\s+(thumb=(["\\']?)([^"\\']+)\\2)?\s*(pic=(["\\']?)([^"\\']+)\\5)?\s*(thumb=(["\\']?)([^"\\']+)\\8)?\]#i', $string, $match)) {
                      for ($i=0; $i<count($match[0]); $i++) {
                          if ($match[3][$i]!=NULL && $match[6][$i]!=NULL) {
                              // print thumb with link to pic
                              $string = str_replace($match[0][$i], "<a href='".$match[6][$i]."'><img src='".$match[3][$i]."'></a>", $string);
                          }
                          elseif ($match[9][$i]!=NULL && $match[6][$i]!=NULL) {
                              // print thumb with link to pic
                              $string = str_replace($match[0][$i], "<a href='".$match[6][$i]."'><img src='".$match[9][$i]."'></a>", $string);
                          }
                          elseif ($match[6][$i]!=NULL) {
                              // just print the pic
                              $string = str_replace($match[0][$i], "<img src='".$match[6][$i]."'>", $string);
                          }
                      }
                  }
                  echo $string;
                  
                  echo "</pre></body></html>";
                  
                  ?>

                  Comment


                  • #10
                    thats really amazing

                    Comment


                    • #11
                      What's that?

                      Comment


                      • #12
                        well Max,... what else can i say now then thank you.


                        so, thank you very much,,... you own!!!

                        best regards

                        Comment


                        • #13
                          seems like a good thread to check url parsing out

                          poop.com
                          http://poop.com
                          http://poop.com/new poop
                          "http://poop.com"

                          poop
                          poop
                          new poop
                          poop
                          "poop"

                          Comment


                          • #14
                            [email protected]

                            Comment


                            • #15
                              oh yea...

                              how does auto html parser work anyway? i know it is with regular expressions... but how?

                              okay, we're searching for the word that starts with http://and then what?

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...
                              X