using subject in php array

Discussion in 'PHP' started by khan11, Sep 22, 2007.

  1. #1
    Hi guys,

    I need some help from you..
    suppose, i have this array:

    $arrayName = array("element a","element b","element c");

    $slicedata = array_slice($arrayName,$start,$end)
    i used foreach($slicedata as $item){ echo $item;};

    i have shown all my elements using <hr> as breaker..
    what i want is to add a subject to all my array elements, the subject of each element should be the first 100 or 50 characters of all array elements..

    Like if element a is:
    this is my data
    blah blah blah

    i want it to show a subject for it also..

    like this is my data as subject
    and
    this is my data
    blah blah blah
    as data in it..


    any suggestions ??
     
    khan11, Sep 22, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    <?php
    
    define('SUBJECT_LENGTH', 100);
    
    // Truncate (ensuring words are not cut off)
    function truncate($item) {
        if (strlen($str) <= SUBJECT_LENGTH)
            return $str;
        
        return strrev(strstr(strrev(substr($str, 0, SUBJECT_LENGTH)), ' ')) . '...';
    }
    
    array_walk($array, 'truncate');
    
    ?>
    PHP:
     
    krt, Sep 22, 2007 IP
  3. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    not working for me :s

    could you please explain how can i use it to display a subject under foreach tags?

    thnx

    EDIT:
    this is my data:

    $sliceda = array_slice($friend,$start,$end);
    foreach($sliceda as $value) {
    echo $spacer . "<div class=postbord>" . "<div align=left class=postmes>Subject</div>" ."<div class=post>" . $value . "</div>" . "</div>" . "<hr>";
    }

    please add your function to it,, i tried to add function in it but it then doesn't display my array!
     
    khan11, Sep 22, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    // Truncate (ensuring words are not cut off)
    function truncate(&$item, $k, $max) {
        if (strlen($item) > $max)
            $item = strrev(strstr(strrev(substr($item, 0, $max)), ' ')) . '...';
    }
    
    $sliceda = array_slice($friend, $start, $end);
    array_walk($sliceda, 'truncate', 50);
    foreach($sliceda as $value) {
      echo $spacer . 
        '<div class="postbord">' . 
        '<div align="left" class="postmes">Subject</div>' .
        '<div class=post>' . $value . '</div>' . 
        '</div>' . '<hr />';
    }
    PHP:
     
    krt, Sep 22, 2007 IP
    khan11 likes this.
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    it worked + goes for you, but one more thing is gone wrong with it now.

    at the place of word "Subject" i wanted to show this 50 character element and the rest of that element under it at $value..

    Now it shows word subject on top and at the place of full value it shows that 50 character element.. :s

    help..
    EDIT:
    I tried to do this, but the problem is, it only shows 1 element in all the cells of my data. The subject is showing ok, but the element is only 1st in all the cells. Check my code to find mistake:

    function truncate(&$item, $k, $max) {
        if (strlen($item) > $max)
            $item = strrev(strstr(strrev(substr($item, 0, $max)), ' ')) . '...';
    }
    krsort($friend);
    $sliceda = array_slice($friend, $start, $end);
    $slicedata = array_slice($friend, $start, $end);
    foreach($slicedata as $valuea) {
    
    };
    array_walk($sliceda, 'truncate', 21);
    foreach($sliceda as $value) {
      echo $spacer . 
        '<div class="postbord">' . 
        '<div align="left" class="postmes">' . $value . '</div>' .
        '<div class=post>' . $valuea .'</div>' . 
        '</div>' . '<hr />';
    };
    PHP:
     
    khan11, Sep 22, 2007 IP
  6. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #6
    i'm waiting for your help guys!!

    see the script above:
    it works fine but i have one problem with it.

    I have array("element a,the data","element b,the data b","element c,the data c")

    by using above function i display:

    element a as subject
    the data as its inside data.

    the script shows proper names of all array elements for subject but for the data, it only shows 1 element in all cells under subjects..

    like:
    element a
    the data of a

    element b
    the data of a

    and so on...


    any ideas??
     
    khan11, Sep 23, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I see, I guessed that "Subject" was a field name.
    // Truncate (ensuring words are not cut off)
    function truncate(&$item, $k, $max) {
        if (strlen($item) > $max)
            $item = strrev(strstr(strrev(substr($item, 0, $max)), ' ')) . '...';
    }
    
    krsort($friend);
    $full = $subjects = array_slice($friend, $start, $end);
    array_walk($subjects, 'truncate', 50);
    foreach($subjects as $k=>$v) {
      echo $spacer .
        '<div class="postbord">' .
        '<div align="left" class="postmes">' . $v . '</div>' .
        '<div class=post>' . $full[$v] . '</div>' .
        '</div>' . '<hr />';
    }
    PHP:
    Well now that I see it, the array_walk is a bit redundant now so maybe try this instead:
    // Truncate (ensuring words are not cut off)
    function truncate($word, $max) {
        if (strlen($word) <= $max) return $word;
        return strrev(strstr(strrev(substr($word, 0, $max)), ' ')) . '...';
    }
    
    krsort($friend);
    $friend = array_slice($friend, $start, $end);
    
    foreach($friend as $v) {
      echo $spacer .
        '<div class="postbord">' .
        '<div align="left" class="postmes">' . truncate($v, 50) . '</div>' .
        '<div class=post>' . $v . '</div>' .
        '</div>' . '<hr />';
    }
    PHP:
     
    krt, Sep 23, 2007 IP
  8. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Solved...

    i was facing problem before but just came to sort it out myself, it was a little word mistake, in truncate u defined $word and in strlen u wrote $item, that was making trouble,

    now it works fine..
    Thumbs up!
    Thank you so much
     
    khan11, Sep 23, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    No worries and thanks for pointing out the mistake.
     
    krt, Sep 23, 2007 IP
  10. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #10
    one more thing required now:

    i'm trying to add this thing also with it but it then disables the character limit..
    $item = preg_replace( "@ +@", " ", $item );
    return str_replace(array(' ', '.'), '-', $item );
    im trying to add it inside function..
     
    khan11, Sep 23, 2007 IP
  11. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #11
    well, im a little student of yours now ;)

    i solved that problem myself by using following thing:

    function maxchars($item, $max) {
    	$fwords = array("'<br>'", "'<br />'");
    	$rwords = array(" ", " ");
    	$item = preg_replace($fwords, $rwords, $item );
        if (strlen($item) <= $max) return $word;
        return strrev(strstr(strrev(substr($item, 0, $max)), ' ')) . '...';
        
       
    }
    PHP:
    i haven't thought myself but got few hints from google and then did my experiment and worked..
     
    khan11, Sep 23, 2007 IP
  12. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #12
    This does all of the 3 things you seem to want:
    - replacing <br>, <br /> and whitespace to one space
    - replacing dots and spaces to hyphens
    function maxchars($item, $max) {
        $item = preg_replace('~(<br( /)?>|\s+|\.)~', '-', $item);
        if (strlen($item) <= $max) return $item;
        return strrev(strstr(strrev(substr($item, 0, $max)), ' ')) . '...';  
    }
    PHP:
     
    krt, Sep 23, 2007 IP