Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找字符串中的所有URL并对查询字符串进行编码?_Php_Url_Curl_Replace_Encode - Fatal编程技术网

Php 查找字符串中的所有URL并对查询字符串进行编码?

Php 查找字符串中的所有URL并对查询字符串进行编码?,php,url,curl,replace,encode,Php,Url,Curl,Replace,Encode,我想在一个字符串(curl results)中查找所有URL,然后对这些结果中的任何查询字符串进行编码,例如 找到的URL: http://www.example.com/index.php?favoritecolor=blue&favoritefood=sharwarma 用编码字符串替换找到的所有URL(我只能执行其中一个) 但要在html curl响应中执行此操作,请从html页面查找所有URL。 提前谢谢你,我已经找了好几个小时了 如果您的CURL结果是一个HTML页面,并且您

我想在一个字符串(curl results)中查找所有URL,然后对这些结果中的任何查询字符串进行编码,例如

找到的URL:

http://www.example.com/index.php?favoritecolor=blue&favoritefood=sharwarma
用编码字符串替换找到的所有URL(我只能执行其中一个)

但要在html curl响应中执行此操作,请从html页面查找所有URL。
提前谢谢你,我已经找了好几个小时了

如果您的CURL结果是一个HTML页面,并且您只需要
a
链接(而不是图像或其他可单击的元素),那么这将满足您的需要

$links
数组将包含页面中URL编码格式的所有链接

编辑:如果您想替换页面中的所有链接,同时保留所有其他内容,最好使用
DOMDocument
而不是正则表达式(related:),下面是我的代码的编辑版本,它用URL编码的等效链接替换每个链接,然后将页面保存到变量中:

$xml = new DOMDocument();

// $html should be your CURL result
$xml->loadHTML($html);

// loop through all "a" elements
foreach ($xml->getElementsByTagName("a") as $link) {
    // gets original (non URL-encoded link)
    $original = $link->getAttribute("href");

    // sets new link to URL-encoded format
    $link->setAttribute("href", urlencode($original));
}

// save modified page to a variable
$page = $xml->saveHTML();

// now do whatever you want with that modified page, for example you can "echo" it
echo $page;

基于的代码。

不要直接使用PHPDOM,它会降低您的执行速度,使用simplehtmldom,它很简单

function decodes($data){
foreach($data->find('a') as $hres){
$bbs=$hres->href;
$hres->__set("href", urlencode($bbs));
}
return $data;
}

使用
preg\u replace\u callback()
对字符串中找到的每个URL调用
urlencode
。为什么我们在您的问题中看不到您的代码?字符串中的URL是否超过1个。。顺便问一下,你能再澄清一下你想做什么吗谢谢大家,php代码(DOM)运行得很好。我现在如何才能让我们说,找到所有的网址再次(包括图片src,css网址,等等),然后改变/替换那里的东西,像感谢你!不要将htmlDOM用于跟踪html。这句话的意思是“如何在文本
而不是html中查找URL
”@MahmoudEskandari
查找字符串中的所有URL(curl results)
查找html页面中的所有URL
清楚地表明OP希望使用html页面,我认为这是最干净的方法。嗨,谢谢你们两位,但是当我添加下面的php时,如果$xxx是我的html结果,我会得到一个空白屏幕…谢谢你们,这似乎很有效。。。刚刚必须完成yum安装php-xml我现在如何让我们假设再次找到所有url(包括图像src、css-url等),然后更改/替换其中的内容
$xml = new DOMDocument();

// $html should be your CURL result
$xml->loadHTML($html);

// loop through all "a" elements
foreach ($xml->getElementsByTagName("a") as $link) {
    // gets original (non URL-encoded link)
    $original = $link->getAttribute("href");

    // sets new link to URL-encoded format
    $link->setAttribute("href", urlencode($original));
}

// save modified page to a variable
$page = $xml->saveHTML();

// now do whatever you want with that modified page, for example you can "echo" it
echo $page;
function decodes($data){
foreach($data->find('a') as $hres){
$bbs=$hres->href;
$hres->__set("href", urlencode($bbs));
}
return $data;
}