如何获取文本中的所有链接(URL)?

时间:2021-04-15 12:35:13

Here is a situation where I can't find a solution. I use this:

这是我无法找到解决方案的情况。我用这个:

// creating ajax object
// ====================

function createRequestObject(){
try { return new XMLHttpRequest() }
catch(e)
{
try { return new ActiveXObject('Msxml2.XMLHTTP') }
catch(e)
{
try { return new ActiveXObject('Microsoft.XMLHTTP') }
catch(e) { return null; }
}
}
}

This is for using Ajax. So I get a text from the PHP file with Ajax (http.responseText). Now I need to find all URL links in text and send another Ajax request to another PHP file that checks a link for availability with cURL reading headers. If server response is 200, then return the HTML link, if other server response or error, then return simple text link.

这是为了使用Ajax。所以我从Ajax(http.responseText)的PHP文件中获取了一个文本。现在我需要在文本中找到所有URL链接,并将另一个Ajax请求发送到另一个PHP文件,该文件使用cURL读取头检查链接的可用性。如果服务器响应为200,则返回HTML链接,如果其他服务器响应或错误,则返回简单文本链接。

Can anyone help how to solve this question?

任何人都可以帮忙解决这个问题吗?

I suppose a JavaScript to work like this:

我想一个JavaScript可以像这样工作:

  1. var PHPreturn = http.responseText.
  2. var PHPreturn = http.responseText。

  3. turn on for() cycle to check for all words divided with backspaces and check all words for a link. if a link suites for the URL regular express, put a new function that will send new Ajax query to another PHP file for checking a link, like:
  4. 打开()循环以检查所有分隔有退格的单词并检查链接的所有单词。如果一个链接套件为URL常规表达,则放一个新函数,将新的Ajax查询发送到另一个PHP文件以检查链接,如:

//turn on cycle that will get all words separated with backspace

//启用循环,将所有单词与退格分开

for(...){

// if a word is a link with http://

//如果单词是http://的链接

if(regular_express == http://){

// replace a word[i] with loading bar, while ajax returns a response from PHP file

//用加载栏替换单词[i],而ajax从PHP文件返回响应

word[i].replace(word[i],'<span id="url_id"><img src="loading.gif"></span>');

// sending Ajax query and waiting for PHP file return

//发送Ajax查询并等待PHP文件返回

// checkURL("PHP file with GET url variable","SPAN ID");

// checkURL(“带有GET url变量的PHP文件”,“SPAN ID”);

checkURL("http_response.php?url="+url,"span_id");


function checkURL(url,place){
var http = createRequestObject();
if(http){
http.open("GET", url);
http.onreadystatechange = function (){
if(http.readyState == 4){
document.getElementById(place).innerHTML = http.responseText;
}
}
http.send(null);
} else {
document.location = url;
}
}

I don't know how all this JavaScript should be. Will appreciate any help. Thank you in advance.

我不知道这些JavaScript应该是怎样的。将不胜感激任何帮助。先谢谢你。

1 个解决方案

#1


0  

Try using responseXML;

尝试使用responseXML;

xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("img");
for (i=0;i<x.length;i++)
{
  // your code here.
}

#1


0  

Try using responseXML;

尝试使用responseXML;

xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("img");
for (i=0;i<x.length;i++)
{
  // your code here.
}