views:

63

answers:

2

I don't know if it is possible! Maybe my thought is also wrong. I want to invoke a search corresponding to all links in my Wordpress blog.

I am using an Ajax call for my other search in this site. How can I retrieve a linking text from hypertext tag in html. eg: <a href='www.example.com'>demo</a>.here i want to get 'demo' as input.

Thanks.

+3  A: 

Try this:

var links_html_list = [];

var links = document.getElementsByTagName('a');

for(var l in links) {
   if(typeof links[i] == undefined) continue;
   links_html_list.push(links[i].innerHTML);
}

function search(term) {
   var results = [];
   for(var l in links_html_list) {
       var cur = links_html_list[l];
       if(typeof cur == undefined) continue; 
       if(cur.indexOf(term) != -1) results.push(cur);
   }
   return (results.length > 0) ? results : null;
}

What the search function does is it loops through the list of strings and if any have the term in it (indexOf), then it will get pushed into an array which is then returned. If there are no matches, it will return null.

Jacob Relkin
+1  A: 

You could use

$homePageText = file_get_contents(file.html);
preg_match_all('/<a .*?>(.*?)<\/a>/',$homePageText,$matches);

Then all anchor text elements will be stored in the array $matches.

Josiah
Thank for your replay.But,this method not help me...which is getting an error like Unknown modifier 'a'
Ajith
Sorry, add a backslash before the "/a" I've edited my posted code.
Josiah