Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Search and Return Div By Class

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #37448
    KelvinAlf
    Member

    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;
    }
    #100477
    KelvinAlf
    Member

    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!

    #100530
    xpy
    Participant

    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…

    #100537
    xpy
    Participant

    @Schmotty ups… wrong paste :$

    #100551
    KelvinAlf
    Member

    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



    Am I doing something wrong?

    #100563
    xpy
    Participant

    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…

    #134291
    Jeremy_RP
    Member

    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;}

    #134755
    DrCLue
    Member

    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 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.