473,789 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 21 '06 #1
4 1523
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
in article 11************* *********@i39g2 00...legr oups.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*=*pre g_replace(*$sea rch,*$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
^^^^^^^
Mar 22 '06 #3
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
Thanks a lot.. It works just fine.. :)

Mar 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2889
by: srinivas reddy | last post by:
Hi, I am reading HTML pages off the web using URL class. I want to decode HTML encoding of accented charatcers in to original form (like &uacute; into accented 'u'). Is there any easy method to do this other than painfully search for each likely instance and replacing it with corresponsing accented character. Your help would be greatly appreciated. tia, Srinivas
8
1606
by: jamesfin | last post by:
Dear XMLers, Please guide me... I have a simple xml file... <URLTest>
9
1679
by: gov | last post by:
Hi, I've just started to learn programming and was told this was a good place to ask questions :) Where I work, we receive large quantities of data which is currently all printed on large, obsolete, dot matrix printers. This is a problem because the replacement parts will not be available for much longer. So I'm trying to create a program which will capture the fixed width
1
1495
by: Fastly | last post by:
Hi Guys I'm no Javascript guru, although am fairly confidant using PHP... What I have knocked together here uses PHP to create a querystring without the iso=XX value, and dump it's output within some Javascript code. The reason for this script is to reload a page with their country settings (i.e. state for us but not for uk) on the form. If iso=uk is already in the querystring we want to strip this from the querystring and use the...
18
4882
by: Atara | last post by:
In my apllication I use the following code: '-- My Code: Public Shared Function strDate2Date(ByVal strDate As String) As System.DateTime Dim isOk As Boolean = False If (strDate Is Nothing) Then isOk = False ElseIf Not (strDate.Length() = 6) Then isOk = False
0
2043
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to parse the information and account for whitespaces, newlines etc. I am hoping that the user enters one per line but that is not always the case. the input would look something like this abc def hij
4
2119
by: almurph | last post by:
Hi everyone, Can you help me please? Say you have hashtable of about 500 key/value pairs. This hashtable has to parse a word. If the word matches the key the then said word must be replaced by the value. My question is though - would parsing occur faster if the word is input as a type string or a type stringbuilder? Do you see my point? Would appreciate any comments/experiences/suggestions that anyone has on this one...
5
1436
by: timslavin | last post by:
Hi, I'm trying to do something with PHP and I'm not 100% familiar with PHP as I am with VBScript. So if you could bear with me on what is likely a stupid question, I'd appreciate it! I have a chunk of text with a variety of tags inside the text. I want to perform the following process to this chunk of text: First, I want to grab data between two markers that I define (e.g.
31
2011
by: David Cramer | last post by:
If you had an application that you were about to begin development on which you wanted to be cross platform (at least Mac and Windows), would you suggest using c++ and Python? I'm asking because we were originally thinking about doing c# but after attending PyCon this year I'm reconsidering. We are already using Python for the website and I figure a c++ backend w/ a Python GUI may work really well, and would be pretty easy to port. ...
2
3725
by: Prabhu Gurumurthy | last post by:
Hello list, I would like to parse IPv6 addresses and subnet using re module in python. I am able to either parse the ipv6 address or ipv6 network but not both using single line. any help appreciated. BTW is there a metacharacter for hex digits. Thanks Prabhu -
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.