Announcement

Collapse
No announcement yet.

Can Someone Help Tweek This Function For Me

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can Someone Help Tweek This Function For Me

    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.
    http://www.sohh.com/index/common/test.php

    This is how the content looks afterI run the function.
    http://www.sohh.com/thewire/read.php?contentID=5886

    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?

  • #2
    You will need to change this preg call, it is checking for the <p> </p> tags:

    PHP Code:
     preg_match_all("/(<p>.+<\/p>)/iUs"$string$array); 

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...
    X