[英]Get Add block after 1st image using preg_replace_callback PHP
[英]Add AdSense Code After The 1st Paragraph Pure PHP
有很多代碼示例說明了如何在WordPress的“ n”段之后添加AdSense代碼。 但是,我找不到用純PHP
制作的項目的實現。
使用jQuery
該任務看起來很原始。 但由於Google可能采取的制裁措施,我試圖避免這種方法。
誰能在沒有任何框架的情況下為我提供一段代碼來應對這樣的任務? 我懷疑提示是使用DOMDocument()
方法。 但是我對PHP的實現並不了解。
例如,我什$html
沒有意識到以下字符串中的$html
應該是什么:
$dom->loadHTML($html)
到底是什么以及我需要解析什么?
假設我有一些函數可以“呼應”我的描述:
function desc()
{
global $data;
foreach ($data as $value)
{
$desc = $value['descr'];
echo $desc;
}
}
我需要在說明中找到第1個(第2個,第3個...)“ p”標簽,然后在其后添加我的AdSense代碼。
誰能協助我完成這項任務?
謝謝!
只需在這種情況下使用explode和for循環即可。
$html = '<p>This is a first paragraph</p>
<p>This is second paragraph</p>
<p>This is third paragraph</p>
<p>This is fourth paragraph</p>';
$content = $html ; //your content or function goes here;
$content = explode("</p>", $content);
$first = 1;
for ($i = 0; $i<count($content); $i++) {
if ($i == $first) {
// put your ads code here
echo $adscode;
}
echo $content[$i];
}
要查找第二段或第三段,可以使用$ i數組索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.