By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Parsing and replacing in PHP

P: n/a
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="resultTitle"><a
href="http://www.xyz.com/guest?setting_key=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="resultTitle"><a
href="http://www.xyz.com/guest?setting_key=files&query=09432400">Yearbook
(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="resultTitle"><input type=checkbox name=checkarray[]
value="35907580"><a
href="http://www.xyz.com/guest?setting_key=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="resultTitle"><input type=checkbox name=checkarray[]
value="09432400"><a
href="http://www.xyz.com/guest?setting_key=files&query=09432400">Yearbook
(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

Mar 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 :-)

Mar 22 '06 #2

P: n/a
in article 11**********************@i39g2000cwa.googlegroups. com, bobzimuta
at ej******@gmail.com wrote on 3/21/06 7:17 PM:
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 :-)

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,*$init ial_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
^^^^^^^
Mar 22 '06 #3

P: n/a
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 );

Mar 22 '06 #4

P: n/a
Thanks a lot.. It works just fine.. :)

Mar 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.