Php 使用preg_match或preg_replace在div中显示图像
我的代码在这里Php 使用preg_match或preg_replace在div中显示图像,php,image,curl,preg-replace,preg-match-all,Php,Image,Curl,Preg Replace,Preg Match All,我的代码在这里 $curl = curl_init($link); $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"; curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CO
$curl = curl_init($link);
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
ini_set('max_execution_time', 300);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
$html= curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$regex = '/<div id="urunid">(.*?)<\/div>/s';
if ( preg_match_all($regex, $page, $regs1, PREG_PATTERN_ORDER) ) {
echo preg_replace('<img(.*)src="([^ "]*)"([^>]*)>', '<a href="$2"><img$1src="$2"$3></a>', "", $regs1[0])."<br />";
} else {
print "Not found";
}
$curl=curl\u init($link);
$userAgent=“Mozilla/5.0(Windows;U;Windows NT 5.1;en-US)AppleWebKit/525.13(KHTML,如Gecko)Chrome/0.A.B.C Safari/525.13”;
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($curl,CURLOPT_USERAGENT,$USERAGENT);
curl_setopt($curl,CURLOPT_TIMEOUT,400);
ini设置(“最大执行时间”,300);
$page=curl\u exec($curl);
if(curl\u errno($curl))//检查执行错误
{
回显“刮片错误:”。卷曲错误($curl);
出口
}
$html=curl\u close($curl);
$dom=新的DOMDocument();
@$dom->loadHTML($html);
$regex='/(.*?)/s';
if(预匹配全部($regex,$page,$regs1,预模式顺序)){
echo preg_replace(']*)>,''','$regs1[0])。“
”;
}否则{
打印“未找到”;
}
为什么我不能在div中显示图像?我只想在div中显示带有链接的图像,但它什么也没显示我不知道为什么。。我的密码错了吗?或者我必须添加另一个函数 试试这个:
$curl = curl_init($link);
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
ini_set('max_execution_time', 300);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
$html= curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$regex = '/<div id="urunid">(.*?)<\/div>/s';
if ( preg_match_all($regex, $page, $regs1, PREG_PATTERN_ORDER) ) {
echo preg_replace('<img(.*)src="([^ "]*)"([^>]*)>', '<a href="$2"><img$1src="$2"$3></a>', $regs1[0])."<br />";
} else {
print "Not found";
}
第一个参数:您正在将']*)>'
作为模式,这是可以的。第二个参数是替换
'
OK第三个参数应该是替换发生时的主语。你给了一个空字符串 我建议您阅读PHP文档,特别是关于“$subject”和返回值如何工作的部分 希望能有帮助
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )