CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2004
    Posts
    23

    Showing part of a string

    Using PHP/MySQL database

    So far this is the code

    Code:
    <?php echo substr($row_rsNews['newsArea'], 0, 150);  ?>.....<a href="news_item.php?newsId=<?php echo $row_rsNews['newsId']; ?>" >(more)</a> <br /><br />
    That shows the first 150 characters, any way of changing that to show the first 22 words.

  2. #2
    Join Date
    Nov 2005
    Posts
    41

    Re: Showing part of a string

    You could split the text into words identified by spaces and fill an array with individual words.

    Use
    $textArray = preg_split ('/ /', $YourString);

    Then you can manipulate the array with array functions.
    For example

    //Number of words in chunk of text
    $show = 22;

    //Extract chunk from array into an array
    $chunk = array_slice ($textArray, 0, $show);

    //Convert array into string
    $chunk = implode($chunk,' ');

  3. #3
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    Thanks for your reply!

    I'm a newbie to PHP

    I assume that these are the three lines I need?
    Code:
    <?php
    //Number of words in chunk of text
    $show = 22;
     
    //Extract chunk from array into an array
    $chunk = array_slice ($textArray, 0, $show);
     
    //Convert array into string
    $chunk = implode($chunk,' ');
     
    ?>
    With $textArray being the in the database I want to cut down.

    In my case $row_rsNewsItems['newsBody']

    I have tried that and it won't work I guess I got it wrong.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Showing part of a string

    Quote Originally Posted by pjb007
    I assume that these are the three lines I need?
    ...
    I have tried that and it won't work I guess I got it wrong.
    That is because those aren't the three lines. You forgot the first one.

    Personally, I wouldn't use preg functions for this.

    PHP Code:
    <?php
    $txt 
    "This is simply a test to show you how to chop off some words.";
    $edited explode(" """$txt);
    $words 10;
    $edited array_slice($edited0$words);
    $edited implode(" "$edited);
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Nov 2005
    Posts
    41

    Re: Showing part of a string

    I have tried that and it won't work I guess I got it wrong.[/QUOTE]

    You first need to convert the block of text you are cutting down into an array using
    $textArray = preg_split ('/ /', $YourString);
    where I have called your block of text $YourString .
    So you need
    $textArray = preg_split ('/ /', $row_rsNewsItems['newsBody'] );

    You may need to wrap your result set in curly braces
    $textArray = preg_split ('/ /', {$row_rsNewsItems['newsBody'] });

    The first parameter in the preg_split function is the regular expression to identify where you want the string to split. I have used a space here because I am assuming you have seperated your words by a space. The backslashes simply escape the quotes.

    If the text in question is only simple text with no regular expressions, then use explode instead of preg_split

    $textArray = explode ( " ", {$row_rsNewsItems['newsBody'] });

    Then the three lines you quoted
    Good Luck

  6. #6
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    Thanks for your replies but I just don't understand how to put it in to my code. I tried both above and can't get them to work.

    I better leave it to removeing characters, I don't like it but it works.
    Last edited by pjb007; June 5th, 2006 at 10:06 AM.

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: Showing part of a string

    Just put the following in place the code you have in your first post.

    PHP Code:
    <?php
    $txt 
    $row_rsNews['newsArea'];
    $edited explode(" """$txt);
    $words 10;
    $edited array_slice($edited0$words);
    $edited implode(" "$edited);
    ?>

    <?php echo $edited;  ?>.....<a href="news_item.php?newsId=<?php echo $row_rsNews['newsId']; ?>">(more)</a><br><br>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    Thanks.

    It won't work, all I get is the ..... (more) link (goes to 404 error)

    My guess is I copyed the table rows down incorrectly before.


    I got somthing a while back so I'm on the right tracks now.

  9. #9
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    Can I ask what the 0 does in that bit of code.

    Should the 10 show 10 words?

    Oh the 404 error problem is fixed!

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: Showing part of a string

    You are correct. The problem lies somewhere in your reading of the database. Keep up updated.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    Here is what I should have put

    Code:
    <?php echo substr($row_rsNewsItems['newsBody'], 0, 150); ?>.....<a href="news_detail.php?newsId=<?php echo $row_rsNewsItems['newsId']; ?>" title="View: <?php echo $row_rsNewsItems['newsTitle']; ?>">(more)</a>
    So using that I got this, and can't see whats wrong, been a long hot day bet its somthing really stupid I've done.

    Code:
    <?php 
    $txt = $row_rsNewsItems['newsBody']; 
    $edited = explode(" ", "", $txt); 
    $words = 10; 
    $edited = array_slice($edited, 0, $words); 
    $edited = implode(" ", $edited); 
    ?> 
    <?php echo $edited; ?>.....<a href="news_detail.php?newsId=<?php echo $row_rsNewsItems['newsId']; ?>">(more)</a><br><br>

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: Showing part of a string

    Okay, we are working with too small of code snippets. Can you just post the whole code for this new feed?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Showing part of a string

    Quote Originally Posted by peejavery
    Okay, we are working with too small of code snippets. Can you just post the whole code for this new feed?
    Isn't the explode function just 2 arguments? explode(" ", $txt); I think you might be thinking of something like str_replace, which takes 3 parameters.

    Instead of: explode(" ", "", $txt)
    It should be: explode(" ", $txt)

    I think
    Last edited by Dr. Script; June 6th, 2006 at 05:25 PM.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  14. #14
    Join Date
    May 2002
    Posts
    10,943

    Re: Showing part of a string

    Quote Originally Posted by Dr. Script
    Instead of: explode(" ", "", $txt)
    It should be: explode(" ", $txt)

    I think
    HAHAHAHAHA! I can't believe I used the command line for str_replace. Thanks Dr. Script. HAHAHAHAHA!
    Last edited by Dr. Script; June 6th, 2006 at 05:26 PM. Reason: to spell explode correctly
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  15. #15
    Join Date
    Oct 2004
    Posts
    23

    Re: Showing part of a string

    It's working!

    Thanks all for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured