Grow your CSS skills. Land your dream job.

PHP Search and Return Div By Class

  • # 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!

    *EDIT*
    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.""; }
    $start=$imgEnd+1;
    }
    $more = 0;
    }
    # April 2, 2012 at 2:52 pm

    It looks like this function is adding a link around all of the images and removing the width and height.

    You could try:



    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++) {
    if (strpos($content, 'class="yourClass"', $start)>=0){
    $imgBeg = strpos($content, '
    $post = substr($content, $imgBeg);
    $imgEnd = strpos($post, '>');
    $postOutput = substr($post, 0, $imgEnd+1);
    //may not need this-> $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
    // or this ->if(stristr($postOutput,''.$postOutput.""; }
    $start=$imgEnd+1;
    }
    }
    $more = 0;
    }


    I added an if statement for the class and changed the img tags to divs

    # 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);
    $start=$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!

    xpy
    # 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();
    preg_match_all($regExp,$code,$results);

    $resultString ='';
    foreach($results[0] as $result)
    {
    $resultString.=$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:01 pm

    @xpy

    in that function you need to replace the class with the variable:

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

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

    Now it’ll search for whatever the class is.

    xpy
    # 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


    /
    ]*class=["']fluid-width-video-wrapper["'][^>]*>(.*?)</div>/

    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?

    xpy
    # 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…

    # April 4, 2012 at 11:56 am

    The function xpy did should work fine(minus the extra echo). You’ll need to call that function from the WP page that loads the content (don’t ask me it’s been too long) when you load the content.

    The parent page will be calling on a function or variable to return a string of content. You’ll need to apply your function to that string first.

    # 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 php.net and look at things like http://php.net/manual/en/domdocument.loadhtmlfile.php

    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 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".