Hi everyone,

First time poster, long time fan. I'm finally posting because I'm struggling with regEx and formatting preg_replace and preg_quote correctly. It's really getting ridiculous how much time I've spent on this, so thank you in advance for any help! I have two types of pattern replacements that I need to do, in order to get my html/php pages to correctly reformat some of the data that is coming in from my xml files:


Example 1 - Reformatting an internal link

Find this pattern:

<a href="../#/filmmaking">

Replace with this format:

<a href="filmmaking.php">

where the word "filmmaking" could be any alphanumeric character,
but everything else is an exact match


Example 2 - Reformatting an image tag

Find this pattern:

<image newX="400" newY="5">images/artwork/example-image.jpg</image>

Replace with this format:

<img src="images/artwork/example-image.jpg" border="0" class="floatleft">

where the values for "newX" and "newY" could be any number,
and where the sequence "artwork/example-image.jpg" could be any directory and file name, with or without a dash or underscore,

but everything else is an exact match

My tired brain thanks you. I would post some of my failed coding attempts, but I think it actually would be more efficient to start with a clean slate here and let someone who really knows what they are doing suggest a direction. This regEx stuff can really mess with your head when you're new to it!

    Regex is difficult like you said, but these are very simple since all you're doing is taking a single chunk from each one and putting it into another string. You may be trying too hard. 😉

    thatsquirrel;10916735 wrote:

    Find this pattern:

    <a href="../#/filmmaking">

    Replace with this format:

    <a href="filmmaking.php">

    where the word "filmmaking" could be any alphanumeric character, but everything else is an exact match

    Try this (I'm using ~ as the outer delimiters, obviously it can be any character you want as long as it's not going to be included in the strings):

    $link1 = '<a href="../#/filmmaking">';
    $link2 = preg_replace('~a href="../#/(.*?)"~', 'a href="$1.php"', $link1); 
    
    echo $link1;
    echo "<br>";
    echo $link2;
    

    Find this pattern:

    <image newX="400" newY="5">images/artwork/example-image.jpg</image>

    Replace with this format:

    <img src="images/artwork/example-image.jpg" border="0" class="floatleft">

    where the values for "newX" and "newY" could be any number,
    and where the sequence "artwork/example-image.jpg" could be any directory and file name, with or without a dash or underscore, but everything else is an exact match

    Try this:

    $image1 = '<image newX="800" newY="5">images/artwork/example-image.jpg</image>';
    $image2 = preg_replace ('~<image.*?>(.*?)</image>~', '<img src="$1" border="0" class="floatleft">', $image1);
    
    echo $image1;
    echo "<br>";
    echo $image2;
    

    This one is a bit of a trick. Since you're not using the values of newX and newY, you don't need to know what (if anything) comes after "&lt;image". 😉

    These are really basic. If they don't make sense, read this tutorial and it will help a lot:

    http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax/page1

    Then re-read my post and it will make sense

      Ossilix,

      Thanks for taking the time to answer! I'm making progress on the image reformatting, thanks to your suggestions. So far, I have found that this code will correctly reformat and display my image in the html/php page:

      <?php
      $find = '<image newX="800" newY="5">images/artwork/example-image.jpg</image>';
      $replace = '<img src="$1" border="0" class="floatleft" />';
      $output = preg_replace('~<image.*?>(.*?)</image>~', $replace, $find);
      echo $output; 
      ?>

      I placed this code chunk, within its own php tags, after the chunk of code that imports and parses my external xml file (the file that contains the image links that need to be reformatted, and other text). When I do it this way, it will reformat and display only the "example-image.jpg" image that is specified in the script, and it places it at the bottom of the page. All the other image tags that appear earlier on the page are still staying in their original format.

      So now I'm figuring out how and where the new code chunk that we just created needs to go, so that it actually applies the reformatting to all of the image tags on the html/php page that match the pattern of the "$find" string.

      I'm sure it must be something simple.... it's just new for me. Any suggestions appreciated, and thanks again for the help!

        thatsquirrel;10916895 wrote:

        I placed this code chunk, within its own php tags, after the chunk of code that imports and parses my external xml file (the file that contains the image links that need to be reformatted, and other text). When I do it this way, it will reformat and display only the "example-image.jpg" image that is specified in the script, and it places it at the bottom of the page. All the other image tags that appear earlier on the page are still staying in their original format.

        That's because you haven't done anything to the page content, you just preg_replaced $find and echoed it at the end of the script (which is why it shows up at the bottom of the page). You need to load the entire contents of the page into a variable and run the preg_replace on that, then echo the content.

        I don't know how you're loading the content from the xml file, but one way to get the page content into a variable that you can modify is output buffering. Here's a tutorial:

        http://www.999tutorials.com/php/how-to-use-output-buffering-in-php/

          Okay, the output buffering stuff is a really helpful clue that I hadn't stumbled upon yet in all that other searches I was doing! This is the code for parsing my xml file, so the output of all this is what I need to run the find/replace function on:

          <?php
          //Initialize the XML parser
          $parser=xml_parser_create();
          
          //Function to use at the start of an element
          function start($parser,$element_name,$element_attrs)
            {
            switch($element_name)
              {
              case "item":
              echo "";
              break;
              case "title":
              echo "";
              break;
              case "description":
              echo "";
              break;
              case "image":
              echo "";
              }
            }
          
          //Function to use at the end of an element
          function stop($parser,$element_name)
            {
            echo "";
            }
          
          //Function to use when finding character data
          function char($parser,$data)
            {
            echo $data;
            }
          
          //Specify element handler
          xml_set_element_handler($parser,"start","stop");
          
          //Specify data handler
          xml_set_character_data_handler($parser,"char");
          
          //Open XML file
          $fp=fopen("xml/page_artwork_Item_Slider.xml","r");
          
          //Read data
          while ($data=fread($fp,4096))
            {
            xml_parse($parser,$data,feof($fp)) or
            die (sprintf("XML Error: %s at line %d",
            xml_error_string(xml_get_error_code($parser)),
            xml_get_current_line_number($parser)));
            }
          
          //Free the XML parser
          xml_parser_free($parser);
          ?> 
          

          So I've been checking out the output buffering tutorial you recommended, and some other links I found..... trying to find similar cases since I learn best by working backwards from examples. Here is one way I have tried integrating my preg_replace code with output buffering and my xml parser. It doesn't work at all (it does display my xml data, but just none of the image reformatting), so as you suggested, I figure I need to find a way to output the result of the xml parser to one variable and then call that variable on the "$find = ??? ;" line. I have a feeling that the code below is conflating a couple approaches. I put question marks and commenting in places where I know for sure I'm screwed up....

          <?php
          
          //Initialize the XML parser
          $parser=xml_parser_create();
          
          //Function to use at the start of an element
          function start($parser,$element_name,$element_attrs)
            {
            switch($element_name)
              {
              case "item":
              echo " ";
              break;
              case "title":
              echo " ";
              break;
              case "description":
              echo " ";
              break;
              case "link":
              echo " ";
              break;
              case "img":
              echo " ";
              }
            }
          
          //Function to use at the end of an element
          function stop($parser,$element_name)
            {
            echo "<br />";
            }
          
          //Function to use when finding character data
          function char($parser,$data)
            {
            echo $data;
            }
          
          //Specify element handler
          xml_set_element_handler($parser,"start","stop");
          
          //Specify data handler
          xml_set_character_data_handler($parser,"char");
          
          //Open XML file
          $fp=fopen("xml/page_artwork_Item_Slider.xml","r");
          
          //Read data
          while ($data=fread($fp,4096))
            {
            xml_parse($parser,$data,feof($fp)) or
            die (sprintf("XML Error: %s at line %d",
            xml_error_string(xml_get_error_code($parser)),
            xml_get_current_line_number($parser)));
            }
          
          function htmlformat_output($buffer)   // ???still not sure where to put this function in relation to the rest of the xml parsing code //
          {
          $find = ??? ;        // ???still trying to figure out what to put here... tried a few things already... //
          $replace = '<img src="$1" border="0" class="floatleft" />';
          $buffer = preg_replace('~<image.*?>(.*?)</image>~', $replace, $find);
          return $buffer;
          }
          
          ob_start("htmlformat_output");
          
          ob_end_flush();
          
          //Free the XML parser       
          xml_parser_free($parser); // ???still not sure where to put this in relation to the rest of the xml parsing code // ?>
            Write a Reply...