Give help. Get help.

  • # April 1, 2012 at 7:59 pm

    Mouthful I know but here is my situation:
    I want to be able to strip everything except a certain div class from the WP content. The structure is so:

    More code in here I want to keep.

    I don't want this whole paragraph.

    Even more code in here I want to keep.

    I got it to semi work except for the fact that it shows the content of the div but not the full div itself.
    What I want it to output is the following:

    More code in here I want to keep.

    Even more code in here I want to keep.

    My PHP isn’t very good at all and I’ve had problems with getting this to work. Any help is appreciated!

    I found a function that does kind of what I want except with images.

    function getVideos() {
    global $more;
    $more = 1;
    $link = get_permalink();
    $content = get_the_content();
    $count = substr_count($content, ' $start = 0;
    for($i=1;$i< =$count;$i++) {
    $imgBeg = strpos($content, ' $post = substr($content, $imgBeg);
    $imgEnd = strpos($post, '>');
    $postOutput = substr($post, 0, $imgEnd+1);
    $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
    if(stristr($postOutput,''.$postOutput.""; }
    $more = 0;
    # April 2, 2012 at 3:42 pm

    Hi Schmotty,

    Thanks for the help! I tried it out and it doesn’t seem to be working. I reworked it just a little bit but here is what I have now.

    function getVideos() {
    global $more;
    $more = 1;
    $content = get_the_content();
    $count = substr_count($content, '
    $start = 0;
    for($i=1;$i< =$count;$i++) {
    if (strpos($content, 'class="fluid-width-video-wrapper"', $start)>=0){
    $vidBeg = strpos($content, '
    $post = substr($content, $vidBeg);
    $vidEnd = strpos($post, '>');
    $postOutput = substr($post, 0, $vidEnd+1);
    $more = 0;

    This is what the original content outputs.

    other text I don't want to keep

    The div with the class of fluid-width-video-wrapper and it’s content is what I am trying to have return.
    Thanks again for the help!

    # April 3, 2012 at 2:39 pm

    Ok… I did this function which uses regular expressions:

    function getDivByClass($code,$class)
    $regExp= "/
    ]*class=["']".$class."["'][^>]*>(.*?)< \/div>/";
    $results = array();

    $resultString ='';
    foreach($results[0] as $result)
    return $resultString;

    It takes two parameters, $code which is as string with the code you want to “strip” and $class which is the class of the div’s you want to keep and returns a String with ‘stripped’ code.

    The function worked for me so I suppose it’ll work for you too…
    This one is for only one class, but with a little modification it could search for more than one…

    # April 3, 2012 at 3:27 pm

    @Schmotty ups… wrong paste :$

    # April 3, 2012 at 5:44 pm

    Thank you guys again! I tried the code out and it doesn’t seem to be working. This is what was output:

    extra text


    The video showing with the rest of the content.

    I ran the function like this

    < ?php getDivByClass(the_content(),'fluid-width-video-wrapper');?>

    Am I doing something wrong?

    # April 4, 2012 at 2:47 am

    First of all, I had an extra ‘echo’ in the function but i removed it so it’s ok now…
    Secondly, $code should be a string with the code you want to ‘strip’, “the_content()” looks like a function…
    Of course, to use the function you should be able to get the content into a string into PHP…
    Otherwise, you may need JavaScript to do it…

    # May 6, 2013 at 7:05 pm

    So you want to only pull divs with select classes to display but not the rest of the paragraphs? There is a simple CSS way to do that. It still retrieves the paragraphs but doesnt display them. If that sounds like it works for you you can do:

    .yourcontainerclass p {display:none;}

    .yourcontainerclass div.yourdivclass {display:block;}

    # May 11, 2013 at 2:11 am

    If the data is coming out of PHP and you want to filter out some while keeping the rest ,
    you might want to go over to and look at things like

    Generally if you poke around there a little , the users will have already posted a pre-made piece you can easily bend to your will.

    Even though I even write custom telephony servers using PHP, when I have some common task , I’ve no shame in picking through the offerings the users post there as if nothing else it will likely save me some of the typing and sometimes it saves me all the typing, allowing the problem to get solved and me more time to play with making 3D Pyramids out of CSS triangles and border images or some other amusement.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.