Forum Moderators: coopster
I need to find all <a href="*"? tags in my result and change them to <a href="javascript:linkClick('*')" where * is the url
I have been using str_ireplace() for all the other changes I have been making and I think that I need to use some form of regexp but I am awful at regular expressions
I also need to make sure that I am not changing any other attributes of <a> while doing this.
Thanks in advance
What I often do is use EditPadPro to test my regex. It has a real nice regex that you can use for testing and it has a free trial. Just google it and you will find where to download.
If it is only a couple of pages I would use EditPadPro to do the work for me. If you have to run this on the fly, then you gotta do something like this:
$newcontent = "";$handle = fopen($filename,"w");
$content = fread($handle,filesize($filename));
foreach(explode("<a ",$content) as $value){
//get the link
preg_match("/href=\"[0-9a-zA-Z\/\:\.]{1,50}\",$value,$matches);
//remove the link
preg_replace("/href=\"[0-9a-zA-Z\/\:\.]{1,50}\","",$value)
//removes the "href="
$href = substring($matches[0],5,length($matches[0]));
$newcontent .= "<a href=\"javascript:linkClick('".$href."')\"";
fwrite($handle, $newcontent);
fclose($handle);
}
totally not tested or clean, but something like that will get you there.
I have an html page that loads a php page through ajax
that php page gets another page on a different server based on the variables passed to it.
I need to go through the content of the php page and change any links that are supposed to open up on the same page to include the javascript function that will ajax them through the php again.
the file writing stuff in your script is throwing me off, would you be able to seperate it out and maybe add a couple comments so I can understand what I am looking at.
I am not a php guy so this is a bit difficult for me thanks
Really what you need sounds like a javascript function that runs on the page load. here is an example:
function setLinks(){
//run onpage load
//get all links for the document
linkObj = document.getElementsByTagName("a");
//cycle through all the links
for(i=0;i<linkObj.length;i++){
//get current link
thisHREF = linkObj[i].href;
//set the new link
linkObj[i].href = "javascript:linkClick('"+thisHREF+"')";
}
}
just tie this to the <body onLoad="setLinks()"> or whatever even loader you have.
PS. not sure if the linkObj[i].href will work like you think. Try changing that line and adding this in if it doesnt work:
thisHREF = linkObj[i].href;
thisObj[i].href = "#";
thisObj[i].onClick = linkClick(thisHREF);
GOOD LUCK!