This is my current code for grabbing some link and image HTML from an off-site link:

<?php

$html = file_get_contents("http://neopets.com/gameroom.phtml");

preg_match("/<a href='\/games\/(.*)>/<img src=(.*) width=100 height=100 border=0>/<\/a>/i", $html, $match);
echo $match[0];
echo $match[1];
?>

Error:

Warning: preg_match(): Unknown modifier '<'

Thanks for any help. 🙂

    ">/<"

    You have undelimited "/"s between ">" and "<" in two places within your pattern

      Your code with an error!

      $html = file_get_contents("http://neopets.com/gameroom.phtml");
      
      preg_match("/<a href='\/games\/(.*)>/<img src=(.*) width=100 height=100 border=0>/<\/a>/i", $html, $match);
      echo $match[0];
      echo $match[1];

      Do this instead with no errors!

      $html = file_get_contents("http://neopets.com/gameroom.phtml");
      
      preg_match("#<a href='\/games\/(.*)><img src=(.*) width=100 height=100 border=0></a>#i", $html, $match);
      echo $match[0];
      echo $match[1];

        you might want to add a question mark to parts of the regex to make it less greedy. This way, you'll be sure to get just the string you're looking for:

        (.?) instead of just (.). This makes it grab the least amount of the string between whatever comes before the parens and what comes after by looking for the very first instance of whatever after the match. In your example, the first (.*?) will match up to the very next >. Without the question mark, you may get unexpected results. Not always, but sometimes. Especially when parsing HTML from tables and such.

        Biggus

          I got it all working now. Thanks.

          <?php
          
          echo "<b>Featured Game:</b><br />";
          $html = file_get_contents("http://neopets.com/gameroom.phtml");
          
          preg_match("#<td width=100><a href='\/games\/(.*)'><img src='http:\/\/images\\.neopets.com\/new_games\/n(.*)' width=100 height=100 border=0></a></td>#i", $html, $match);
          echo $match[0];
          ?>

          I have two last questions: How can I edit the preg_match result? Because my site's valid XHTML, and I want to make the result valid XHTML too...

          And how would I get PHP to submit a form for me without actually getting me to press the submit button?

            $match[0] can me modified like any string. Modify it any way you want.

            To fake a POST, you need to pass the info in a header using fsockopen() , etc.

            Biggus

              But if I modify it the normal way, won't I lose the preg_match wildcard?

                if you modify $match[0] directly, yes. So, use another variable, say $tmp:

                $tmp=makeXHTML($match[0]);
                

                Where makeXHTML() is your function which ensures that the string is valid XHTML.

                Did that answer your question, or did I misunderstand you?

                Biggus

                  I put everything in a function, with preg_replace:

                  function makeXHTML () {
                  
                  $html = file_get_contents("http://neopets.com/gameroom.phtml");
                  
                  preg_match("#<td width=100><a href='\/games\/(.*)'><img src='http:\/\/images\\.neopets.com\/new_games\/n(.*)' width=100 height=100 border=0></a></td>#i", $html, $match);
                  
                  echo $match[0];
                  
                  $search = array ("width=100",
                                           "height=100",
                                           "border=0");
                  
                  $replace = array ("width='100'i",
                                            "height='100'i",
                                            "alt='NAPortal'i");
                  
                  $replace = preg_replace($search, $replace, $match[0], 1);
                  
                  echo $replace;
                  
                  //$tmp = makeXHTML($match[0]); 
                  }
                  
                  makeXHTML();

                  Error:

                  Warning: preg_replace(): Delimiter must not be alphanumeric or backslash

                    4 years later
                    preg_match("#<td width=100><a href='\/games\/(.*)'><img src='http:\/\/images\\.neopets.com\/new_games\/n(.*)' width=100 height=100 border=0></a></td>#i", $html, $match);

                    Because you're using # for your pattern delimiter, you don't need to escape the '/'.

                    Warning: preg_replace(): Delimiter must not be alphanumeric or backslash 

                    None of your patterns (the elements of the $search array) have delimiters, which preg_replace() is expecting for the first character; and 'w', 'h', and 'b' can't be used as delimiters.
                    But [man]str_ireplace[/man] would be more efficient there, anyway.

                      Write a Reply...