Main Topics
Browse All TopicsHi X-perts,
I have a raw html output as a php string $div. I need to transform ALL the links found in the string as follows:
1) current (existing link)
<a href ="index.php?some variables...">
2) a new transformed link:
<a href="#" onClick="myfunction('index
Could someone please, suggest a clean and simple code for this replacement using php only? It would be easy to use jscript, but I cannot use it here.
I think the easiest way would be to use preg_replace(), but cannot figure out how to build a reg expression.
Please, help.
Thanks
A
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
ed987,
I tried your code, but it does not replace anything. This is the simplified example of what i need:
current link:
<a href="index.php?main_page=
converted link:
<a href="#" onClick="myfunction('index
==========================
As I mentioned, the point is to make myfunction() with the same argument as the href value
Here is a sample of the html code to be transformed. I have checked ed987's code and it seem to be working, though I need to test it more on different links.
One question - how to add a filter to the regex to replace ONLY links containing substring 'index.php' or index2.php? There could be some links to outside url's, which should not be modified
Thank you guys - I am getting closer. The us111's regex works OK and replaces ALL links, i.e.
$div2=preg_replace('/<a[ \t\n\r]*href[ \t\n\r]*=[ \t\n\r]*"([^"]*)"([^>]*)>/
Next, I am trying to add a filter to replace only links containing main_page=index or main_page=product_info
Here is my code attached:
It does not work. What is wrong here? I am placing the fixed string main_page=index|main_page=
Where am I making the mistake here?
Business Accounts
Answer for Membership
by: ed987Posted on 2007-12-28 at 02:07:29ID: 20540758
Select allOpen in new window