Page 1 of 1

strange requirement with PHP

Posted: Sat Feb 21, 2009 10:16 pm
by hemanshurpatel
hello friends,
i have recently started coding PHP, so i am still at beginners stage, so please bear me if you found this very silly question.

What i want is to add a particular code after say first 100 characters in my post.

say i have a post of 1000 words and after initial say 100 words i want to add a new tag of ream more, that tag is <!--more-->.
so after first 100 characters this tag should come and then the rest of post should.
so is there any function in PHP which let me allow few words in somewhere middle of any post?

Waiting for the reply and thanking of you in advance.

Regards,

Hemanshu Patel
http://www.tutorialsforu.info
http://www.articlesdir.co.cc

Re: strange requirement with PHP

Posted: Sat Feb 21, 2009 10:28 pm
by requinix
You can use substr_replace with a $length of zero...

Re: strange requirement with PHP

Posted: Sun Feb 22, 2009 1:26 am
by hemanshurpatel
i think you didnt get my problem

how can use substr_replace
i dont know what i need to replace with, as i dont know what word it will be at the end of 100 characters.

Re: strange requirement with PHP

Posted: Sun Feb 22, 2009 1:47 am
by semlar
Okay, you've said two very different things in your post, so if you could just clear up whether you want to insert your text after the 100th character or the 100th word that would help immensely. I'm assuming you mean characters, because that makes more sense.

Here's an example for using substr_replace..

Code: Select all

<?php
$string = "1234567890";
echo substr_replace($string,'[!-- example --]',5,0);
?>
Outputs

Code: Select all

12345[!-- example --]67890
You would just use something like substr_replace($string,'<!-- more -->',100,0)

Re: strange requirement with PHP

Posted: Sun Feb 22, 2009 4:41 am
by hemanshurpatel
yeah, thats what exactly i want
let me try this