Forum Moderators: coopster

Message Too Old, No Replies

Adding information before and after href url

trying to add information before and after href url

         

ngrant

8:45 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



I am working on a project and I have come across a problem.

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

willis1480

9:29 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



best bet is to learn a little regex, youll need sooner than later. use preg_replace.

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.

ngrant

11:03 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



yeah it has to run on the fly. I have sort of a monstrosity right now and If I would have known ahead of time that this project would get so ugly I wouldn't have agreed to do it.

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

willis1480

12:28 am on Nov 18, 2008 (gmt 0)

10+ Year Member



SOrry, ignore that fwrite anyway. I was just giving you an idea of reading a string in from file and parsing it.

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!

willis1480

1:05 am on Nov 18, 2008 (gmt 0)

10+ Year Member



oops, made mistake in my code...thisObj[i] should be linkObj[i]

sorry

ngrant

4:56 am on Nov 18, 2008 (gmt 0)

10+ Year Member



I can't use the page load because the main page is loaded once and loads the subpages (which I have no control over) with ajax so on page load would only work for the first one