473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PHP/REGEXP to inject data into html string

3 New Member
Salutations!

I have an odd situation and know nothing about regular expressions, but I believe I will need to use them to attempt what I have to do.

I am running a script in PHP 5 that takes some user input, formats and sends that input to a completely different server and puts that server's reply -- basically the entire returned page -- into a string. I have the entire contents of the resulting HTML page in a string called $ReturnedString. All this works properly. I can echo the string to the user and it displays the page the other server sent my script.

Now, here is where I get stuck. I want to be able to find the href="" inside <A> tags ONLY and inject a static string between the first double-quote and whatever the href might contain, then save the entire modified "page" back to the original $ReturnedString, or another string if necessary.

Any suggestions? Be kind to me, my coding skills are quite poor. :)
Oct 7 '06 #1
3 2130
ronverdonk
4,258 Recognized Expert Specialist
Is this of use?

[php]
<?php
$text = 'This is a text and a <a href="linkto.php?id=2">link</a>';
// find <a ..... </>yyy</a> tags
$pattern = "/<a.*? href=(\"|')(.*?)(\"|').*?>(.*?)<\/a>/i";
preg_match_all($pattern, $text, $matches);
echo '<pre>'; print_r($matches);
?>[/php]

Ronald :cool:
Oct 7 '06 #2
inanis
3 New Member
Thanks for the code.

It looks like we are going in the right direction. We get a cool array that contains pretty much all the stuff between the <a> tags, but its not quite what I am looking for.

The generated array only has the information from inside the <a> tags, and from what I can tell does not contain any of the html from anywhere else in the page.

I guess I dont really need a seperate array of the <a>'s and their content, but rather just need to be able to locate the HREFs inside <a>'s in a given string and inject a string directly between the href=" and everthing after it. For example, given a string containing the following html:
[PHP]
<html>
<body>
<a target="_top" id="foo" href="someplace.htm"></a>
<a target="_top" id="foo" href="anotherplace.htm"></a>
</body>
</html>
[/PHP]

I would like the final output string generated by the php code to be:

[PHP]
<html>
<body>
<a target="_top" id="foo" href="somecgi.cgi?u=someplace.htm"></a>
<a target="_top" id="foo" href="somecgi.cgi?u=anotherplace.htm"></a>
</body>
</html>
[/PHP]

Notice the "somecgi.cgi?u=" injected between the doublequote and the text after it.

Your thoughts?
Oct 8 '06 #3
inanis
3 New Member
I believe I figured it out. I used part of your regexp along with a preg_replace and the \\0 backreference.

It is working the way I want, I believe.

THANKS!
Oct 8 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
3
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some...
3
by: Russell | last post by:
hey, I'm struggling trying to get the concepts of the regExp function down.... What i'm trying to achieve is to remove all white space from html formatted source code. I have the following...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
4
by: r | last post by:
Hello, It seems delimiters can cause trouble sometimes. Look at this : <script type="text/javascript"> function isDigit(s) { var DECIMAL = '\\.'; var exp = '/(^?0(' + DECIMAL
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.