How to use add ads into the middle of Joomla php article.
1. To show modules borders on page. Extension→Templates→Options→Preview Module Positions set Enabled
2. At the end of URI add ?tp=1
3. I have to add positions to template for Ads.
3.1. Add new position-100 in TemplateDetails.xml It makes new position -100 available to assign from admin menu of module.
3.2. Into index.php add new <div> with <jdoc:include type="modules" name="position-100" />
It works. So create new module assign it to pos-100.
4.1 I have to use file /templates/[name-of-template]/html/com_content/article/default.php // article text contained in $this→item→text;
4.2 Add functiont to the file mentioned above. Function will divide text by <p> fragments, count it and insert our code with addverticement.
function adds($articleText, $code) {
$countP = substr_count($articleText, ‘<p>’); // Count how many paragraphs in text.
// We will add adverticement in each 6-th paragraph
$start=0;
if($countP<12){ // If article is too short
return $articleText;
}
elseif($countP<21){ // If article is short
$howmuch=1;
$positionP=(int)($countP/2);
for($i=0; $i<$positionP; $i++){
$pos = stripos($articleText, ‘<p>’, $start);
$start = $pos + 3;
}
$first = substr($articleText, 0, $pos); // divide article to parts
$second = substr($articleText, $pos); // divide article to parts
return $first.$code.$second;
}
else{ // If article is long
$howmuch=3;
$positionP=(int)($countP/3); // whole number division. Max 3 adds in an article.
for($i=0; $i<$countP; $i++){
$temp=stripos($articleText, ‘<p>’, $start);
$start = $temp + 3;
if($positionP==$i){
$pos1 = $temp;
}
if($positionP*2==$i){
$pos2 = $temp;
}
}
$first = substr($articleText, 0, $pos1); // divide article to parts
$second = substr($articleText, $pos1, $pos2); // divide article to parts
$third = substr($articleText, $pos2); // divide article to parts
return $first.$code.$second.$code.$third;
}
} // function ends
4.3. Add to article text $this->item->text; // $this→item→text = adds($this→item→text, ‘<div>CODE</div>’);