Hi!
I get the message that ereg_replace() is deprecated.

  1. this is the example code (i used this code to learn how to use preg_replace)
    <?php
    $string = "Subtitle of first. <!-- [-list_ini-] -->Paragraph<!-- [-list_fin-] -->. The second paragraph";
    echo "Original Text: <br />".$string."<br />------------<br />";
    
    $output1 = ereg_replace(".*<!-- \[-list_ini-\] -->(.*)<!-- \[-list_fin-\] -->.*","\\1",$string);
    echo "Using ereg_replace: <br />".$output1."<br />-------------<br />";
    
    $output2 = preg_replace("#.*(<!-- \[-list_ini-\] -->)(.*)(<!-- \[-list_fin-\] -->).*#e","'$2'",$string);
    echo "Using preg_replace: <br />".$output2."<br />";
    
    //old funcion shows: 'Paragraph', it means: '$2', and preg_replace too...
    ?>
    1. So, I want to change the following code, to use preg_replace... but didn't work!
      In this case, i don't know why the función don't return only $2;
      Returns: text before ($1), text that i need [the row] ($2), and the text after($3)
    <?php
    $string = '
    <table align="center" width="775" id="tabla_gral">
    <tr class="fila_titular"> 
      <th width="50">Id</th>
      <th width="150">Nombre</th>
    </tr>
    
    <!-- [-listado_ini-] --> 
    <tr class="fila_contenido [-fila-]"> 
      <td width="50" class="centro"[-editando_auxiliar-]>[-id-]</td>
      <td width="150">[-nombre-]</td>
    </tr>
    <!-- [-listado_fin-] -->
    
    </table>';
    echo "Original Text: <br />".$string."<br />------------<br />";
    //--------------------------------------------------------
    $listado1 = ereg_replace(".*<!-- \[-listado_ini-\] -->(.*)<!-- \[-listado_fin-\] -->.*","\\1",$string); // ereg_replace deprecated
    echo "Using ereg_replace: <br />".$listado1."<br />-------------<br />";
    
    $listado2 = preg_replace("#.*(<!-- \[-listado_ini-\] -->)(.*)(<!-- \[-listado_fin-\] -->).*#e","$2",$string);
    echo "Using preg_replace: <br />".$listado2."<br />-------------<br />";
    
    /* i want that $listado has "only" the row:
    <tr class="fila_contenido [-fila-]"> 
      <td width="50" class="centro"[-editando_auxiliar-]>[-id-]</td>
      <td width="150">[-nombre-]</td>
    </tr>
    */
    ?>
    if somebody help me... i will be happy!!
    thanks for advance!!
    Andrea

    I got it working.

    This is pattern. I think you understand:

    "#(.*)<!-- \[-listado_ini-\] -->(.*)<!-- \[-listado_fin-\] -->(.*)#s"

    There are 3 catch: (.*) .... this is what you want.

    Important: #s" at the end
    We need this 's' if we have newlines in the $string (\n)
    But not if you only have a string like this:
    "this is some text with not any newlines"

    "this is some text with one
    newline"
    Here is my test. It works.

    <?php
    $string = '
    <table align="center" width="775" id="tabla_gral">
    <tr class="fila_titular">
    <th width="50">Id</th>
    <th width="150">Nombre</th>
    </tr>
    
    <!-- [-listado_ini-] -->
    <tr class="fila_contenido [-fila-]">
    <td width="50" class="centro"[-editando_auxiliar-]>[-id-]</td>
    <td width="150">[-nombre-]</td>
    </tr>
    <!-- [-listado_fin-] -->
    
    </table>';
    
    echo nl2br(htmlspecialchars($string))."<br /><br />";
    
    $pattern = "#(.*)<!-- \[-listado_ini-\] -->(.*)<!-- \[-listado_fin-\] -->(.*)#s";
    $listado2 = preg_replace($pattern, "$1$2$3", $string);
    
    echo "Using preg_replace: <br />".nl2br(htmlspecialchars($listado2));
    
    ?>

      Halojoy! Thank you very very much!!!

      there was the #s insted #e!!

      i'm starting with this regular expressions and i understand very basic language...
      thank you for your time!!
      kisses!! andrea
      ps: i chaged a lot of times the syntaxis before post it, and i had forgotten to remove the brackets (1 and 3), but this part i had understood good! thanks again!

        Write a Reply...