Parsing and replacing in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • niftyhawk@gmail.com

    Parsing and replacing in PHP

    Hi All,

    I have the HTML in "Output A" generated dynamically and stored in a PHP
    variable say $contents.

    I want this to parse and insert checkboxes in the code which have to
    look like "OUTPUT B". The checkbox value should be the value of
    "query=" in the "href tag".

    OUTPUT A
    =========

    <table width="100%">
    <tr><td align="left" valign="top" width="20px">1. &nbsp;</td>
    <td align="left" colspan="3">
    <span class="resultTi tle"><a
    href="http://www.xyz.com/guest?setting_k ey=files&query= 35907580">Cats
    (Port Orange, Fla.)</a></span><br />
    <tr>
    <td colspan="4">
    <img src="http://www.xyz.com/website/images/backgroundPixel .gif"
    width="100%" height="1" alt="divider" />
    </td>
    </tr>
    <tr><td align="left" valign="top" width="20px">2. &nbsp;</td>
    <td align="left" colspan="3">
    <span class="resultTi tle"><a
    href="http://www.xyz.com/guest?setting_k ey=files&query= 09432400">Yearb ook
    (Cat Fanciers' Association)</a></span><br />
    <tr>
    <td colspan="4">
    <img src="http://www.xyz.com/website/images/backgroundPixel .gif"
    width="100%" height="1" alt="divider" />
    </td>
    </tr>
    </table>


    OUTPUT B
    =========

    <table width="100%">
    <tr><td align="left" valign="top" width="20px">1. &nbsp;</td>
    <td align="left" colspan="3">
    <span class="resultTi tle"><input type=checkbox name=checkarray[]
    value="35907580 "><a
    href="http://www.xyz.com/guest?setting_k ey=files&query= 35907580">Cats
    (Port Orange, Fla.)</a></span><br />
    <tr>
    <td colspan="4">
    <img src="http://www.xyz.com/website/images/backgroundPixel .gif"
    width="100%" height="1" alt="divider" />
    </td>
    </tr>
    <tr><td align="left" valign="top" width="20px">2. &nbsp;</td>
    <td align="left" colspan="3">
    <span class="resultTi tle"><input type=checkbox name=checkarray[]
    value="09432400 "><a
    href="http://www.xyz.com/guest?setting_k ey=files&query= 09432400">Yearb ook
    (Cat Fanciers' Association)</a></span><br />
    <tr>
    <td colspan="4">
    <img src="http://www.xyz.com/website/images/backgroundPixel .gif"
    width="100%" height="1" alt="divider" />
    </td>
    </tr>
    </table>

    Can anybody help me with a code sample as to how to do this?

    Thanks

  • bobzimuta

    #2
    Re: Parsing and replacing in PHP

    Easiest way is to use a perl regular expression search and replace.

    Something like this?



    I'm feeling nice (and bored) today :-)

    Comment

    • Stephen Kay

      #3
      Re: Parsing and replacing in PHP

      in article 1142986667.3297 86.202020@i39g2 00...legr oups.com, bobzimuta
      at ejmalone@gmail. com wrote on 3/21/06 7:17 PM:
      [color=blue]
      > Easiest way is to use a perl regular expression search and replace.
      >
      > Something like this?
      >
      > http://em.hopto.org/code/test/test_preg.php
      >
      > I'm feeling nice (and bored) today :-)
      >[/color]


      Thanks, that was helpful for the thing that I am working on, too.

      However, maybe you can answer this - if I have a string formatted into a
      variable, say $strip_it, which is:

      $strip_it = "http://www.someaddress .com/somepage";

      ....and I wanted to strip this out of a whole page of code where it appeared
      multiple times, how would you set the values?


      From your example (note I am using PHP 4.4, so no count parameter):


        $search  =  $strip_it;
        $replace = ""; // replace with nothing
        
        $result = preg_replace( $search, $replace, $initial_html, -1, );

      I can't figure out how to set up $search - I keep getting different
      errors...

      --
      Stephen Kay
      Karma-Lab sk@karma-lab.NOSPAM.com
      ^^^^^^^


      Comment

      • bobzimuta

        #4
        Re: Parsing and replacing in PHP

        Yeah, you would get errors since $strip_it contains the '/' characters.
        They have special meaning for the preg functions. If you ever become
        interested, learn all about regular expressions. They are very fun!

        In order to just remove $strip_it from all the text, try the
        str_replace function. It'll be faster, too.

        $result = str_replace( $strip_it, '', $initial_html );

        Comment

        • niftyhawk@gmail.com

          #5
          Re: Parsing and replacing in PHP

          Thanks a lot.. It works just fine.. :)

          Comment

          Working...