Jump to content

Change text output position of PHP


brendas

Recommended Posts

Hello,

 

This code add ads in the content. I want to change the output. I tried on it but I could not solve the problem.

Thanks.

add_filter( 'the_content', 'ads_paragraphs');
function ads_paragraphs( $content ) {

$adsbeforeparagraph = array(1,3,5);

global $post;

$ad = 'ADS CODE';

$content_expl = explode("<p>", $content);
for ($i = 0; $i <count($content_expl); $i++ ) {
if (in_array($i, $adsbeforeparagraph)){
$content_expl[$i] = $ad . $content_expl[$i];
}
} 

return implode("<p>", $content_expl);
}

input:

<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>
<p>text text text</p>

output:

<p>ADS CODEtext text text</p>
<p>text text text</p>
<p>ADS CODEtext text text</p>
<p>text text text</p>
<p>ADS CODEtext text text</p>
<p>text text text</p>

I want this output:

ADS CODE<p>text text text</p>
<p>text text text</p>
ADS CODE<p>text text text</p>
<p>text text text</p>
ADS CODE<p>text text text</p>
<p>text text text</p>
Link to comment
Share on other sites

You could use array_walk


$ad  = 'ADS CODE';

$data = [ '<p>test test test</p>',
          '<p>test test test</p>',
          '<p>test test test</p>',
          '<p>test test test</p>',
          '<p>test test test</p>',
          '<p>test test test</p>'   ];

array_walk ($data, 'add_prefix', $ad);


function add_prefix (&$item, $key, $prefix)
{
	if ($key%2 == 0) {
	    $item = $prefix . $item;
	}
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.