【问题标题】:how to replace img url in a array如何替换数组中的图片url
【发布时间】:2015-02-28 11:39:57
【问题描述】:

我正在从 ulr 获取数据,所以现在想更改图像的 url,我的代码是

@$domain = $_REQUEST['domain'];
function get_dns($url, $curl = true) {
    $responseString = '';
    if (!$curl) {
        $responseString = file_get_contents($url);
    } else {
        $ch = curl_init( $url );
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array('Content-Type: text/html', 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36') , 

        );
        curl_setopt_array( $ch, $options );
        $responseString =  curl_exec($ch);
    }
    return $responseString;
}

$url ="http://intodns.scriptburn.com/index.php?domain=$domain";
$query = get_dns($url);
preg_match_all('/<!-- BEGIN CONTENT -->(.*?)<!-- END CONTENT -->/s', $query, $content);
echo $content = $content[0][0];

这里我想将&lt;img src="static/images/ 替换为&lt;img src="https://www.whoisextractor.in/wp-content/themes/whois/img/

【问题讨论】:

  • 如果你想用另一个替换一个众所周知的字符串,为什么不使用str_replace
  • 您还应该考虑使用 HTML 解析器

标签: php html curl


【解决方案1】:

https://stackoverflow.com/a/14575485/2384642的帮助下,我终于找到了自己的解决方案

现在我的代码是

    $url ="http://intodns.scriptburn.com/index.php?domain=$domain";
    $query = get_dns($url);
    preg_match_all('/<!-- BEGIN CONTENT -->(.*?)<!-- END CONTENT -->/s', $query, $content);
    $content = $content[0][0];
    $content = preg_replace_callback('~<img\ssrc="static/images/~i', 
    function($m)
    {
        return sprintf('<img src="https://www.whoisextractor.in/wp-content/themes/whois/img/', urlencode($m[1]), $m[2], $m[3]);
    }, $content);
    echo $content;

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 2017-08-01
    • 1970-01-01
    • 2021-08-08
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多