I have a function that I used to read through a HTML formatted string and insert an ad in the string. Basically, it converts the string into an array based on the <p> and </p> tags.
Here is the function:
function insert_ad($string,$code,$split) {
$split--;
preg_match_all("/(<p>.+<\/p>)/iUs", $string, $array);
$prefix_ARRAY = array_slice($array[1], 0, $split);
$prefix_ARRAY[$split] = $code;
$suffix_ARRAY = array_slice($array[1], $split);
$new_array = array_merge($prefix_ARRAY, $suffix_ARRAY);
$new_string = implode("\n\n", $new_array);
return $new_string;
}
$code would be the AD CODE...Like JAVASCRIPT.
$split would be where I want the $code inserted at.
This is how the content looks before I run the function.
This is how the content looks afterI run the function.
NOW, the problem is that it only works with <p> and </p>. If you look at the source of the BEFORE link, you'll notice that I had to put the <ul> tags inbetween the <p> tags to get it to work. Otherwise, nothing will show up. It appears that anything not inbetween <p> get's thrown out.
Any sugguestions?
Here is the function:
function insert_ad($string,$code,$split) {
$split--;
preg_match_all("/(<p>.+<\/p>)/iUs", $string, $array);
$prefix_ARRAY = array_slice($array[1], 0, $split);
$prefix_ARRAY[$split] = $code;
$suffix_ARRAY = array_slice($array[1], $split);
$new_array = array_merge($prefix_ARRAY, $suffix_ARRAY);
$new_string = implode("\n\n", $new_array);
return $new_string;
}
$code would be the AD CODE...Like JAVASCRIPT.
$split would be where I want the $code inserted at.
This is how the content looks before I run the function.
This is how the content looks afterI run the function.
NOW, the problem is that it only works with <p> and </p>. If you look at the source of the BEFORE link, you'll notice that I had to put the <ul> tags inbetween the <p> tags to get it to work. Otherwise, nothing will show up. It appears that anything not inbetween <p> get's thrown out.
Any sugguestions?
Comment