Say I have a page of text attached to $page, so $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind'; Code (markup): Does anyone know how I can split it up into an $intro and a $main, so the $intro bit displays the first 10 words and the $main display everything but the first 10 words? I tried doing a truncate type thing to get the $intro, then eregi_replace to remove the content of $intro from $page to make $main, but it didn't really work properly. So I thought there might be a better way, any ideas?
Is this dynamic? If not, why don't you have 2 variables? $intro = 'the intro text'; $main = $intro.' even more text!'; PHP: or do something like: $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind'; $splits = explode(" ", $page); array_splice($splits, 10); $intro = implode(" ", $splits); PHP:
Don't know how to get that wit array_splice, but use preg_replace instead; $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind'; $splits = explode(" ", $page); array_splice($splits, 10); $intro = implode(" ", $splits); $main = preg_replace("/$intro/i", "", $page); PHP: Tested it
The slashes in the preg_replace functions as delimiter and the i to ignore case sensitivity. This whole process is called 'RegEx'. (Regular Expressions)
if you want to split text into multiple pages(dynamicly) I have the script for it. by the way this is what you need: <?php $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind'; $splits = explode(" ", $page); $count = count($splits); $half = round($count / 2) ; $arrayslice = array_splice($splits,0,$half,$else); $firsthalf = implode(" ",$arrayslice); $secondhalf = implode(" ",$splits); echo $firsthalf;//firsthalf echo "<br/>"; echo $secondhalf;//secondhalf ?> PHP:
Thanks astrazone, I will try that too. I was just looking over my previous posts and realized I didn't reply to you.
substr(); is your answer! $string = "ALL YOUR TEXT"; $start = 0; $end = 10; echo substr($string, $start, $end); echo substr($string, $end, 6000); Code (markup):
yuvrajm, that works too - I gave you a plus rep also. Now if I could figure out how to make it so I can put code 10 in from the end instead of 10 characters from the beginning that would be uber cool.
This works really well, but how can I alter it it to count words instead of characters, or even just count white spaces so it doesn't chop words in half? I know all the examples do that but this substr method seems the easiest so far.
got this solution from a website. This $no will count words, but not whitespaces... for that you may simple make 2x of $no so whitespace may also be counted. I have not tested thus one till now. function count_words($str) { $no = count(explode(" ",$str)); return $no; } Code (markup):
Nope you need something like: preg_match('#((?:\S+\s+){10})(.*)#s', $page, $matches); $header = $matches[1]; $main = $matches[2]; PHP: Good Luck!
Agreed! Actually I'm a little starter in regular expressions. You are right, this is the best way to count chars in a string.
Well I can't even understand what that one's doing so I don't know how to get it to do the insert logo/ad thing after so many words?