|
-
June 5th, 2006, 03:25 AM
#1
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.
-
June 5th, 2006, 06:36 AM
#2
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,' ');
-
June 5th, 2006, 08:26 AM
#3
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.
-
June 5th, 2006, 09:30 AM
#4
Re: Showing part of a string
 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($edited, 0, $words);
$edited = implode(" ", $edited);
?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 5th, 2006, 09:49 AM
#5
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
-
June 5th, 2006, 10:03 AM
#6
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.
-
June 5th, 2006, 10:16 AM
#7
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($edited, 0, $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.
-
June 5th, 2006, 10:47 AM
#8
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.
-
June 5th, 2006, 10:55 AM
#9
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!
-
June 5th, 2006, 10:56 AM
#10
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.
-
June 5th, 2006, 11:04 AM
#11
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>
-
June 5th, 2006, 11:26 AM
#12
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.
-
June 5th, 2006, 01:51 PM
#13
Re: Showing part of a string
 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
-
June 5th, 2006, 02:47 PM
#14
Re: Showing part of a string
 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.
-
June 6th, 2006, 03:20 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|