/    Sign up×
Community /Pin to ProfileBookmark

cut the first X character from this variable without counting html tags,How

Hi Guys ,

I have a vriable contain text with html tags

I want to cut the first 200 character from this variable without counting html tags .

What is the best method to do that ?

I suggest the folwing method :

1- move the text to another variable with strip_tags function .

2- get the first 200 character from new variable and put it in $mirror_var .

3- find the last word in $mirror_var .

4- get the the characters from main variable upto lastword .

But I have a problem in finding last word in $mirror_var

I want to know what is the best method to find last word in variable contain long text ?

OR

if there is another method better than my method

Cheers

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@HellspireSep 13.2004 — Okay lets approach the matter academically.....

first off trim() the variable, then use [b]something[/b] like this... note that your attempt is slightly complicated and i dont have the resources here with me to help you at the moment.

$string = trim($_POST[string]); // assuming it comes from a form...

$num_chars_stripped = 0;

while($num_chars_striped <= 200){

// regular expression strip here...

}

Your regular expression will need to, determine if character starts an html tag example < and then it will have to find out the length between < and the next > and then move the pointer to the slot after the html tag, and start again, each character that is removed is $num_chars_stripped++;

Once again... I cant help you at the moment, however, to get the last word of the variable i suggest parsing the string dividing the words, and then do $m = count($array); then $array[$m] is your last word....

good luck...
Copy linkTweet thisAlerts:
@evil_mooseSep 13.2004 — The problem you might encounter if you are using the last word in the string, is that it is possible that that word exists earlier in the string therefore cutting the string at the wrong position.

Just at the top of my head, couldn't you write a loop that figured out what the correct index to cut at was by trundling through the string on character at the time incrementing a counter on valid character only (thus ignoring tags). Ok, it might not be the quickest way and it is a bit dirty, but is the only one I can think of right now.

Anyway, might be of some help. ?

? Bit slow with my posting, as Hellspire got there before me, anyway we are thinking in the same manner here. ?
Copy linkTweet thisAlerts:
@HellspireSep 13.2004 — evil_moose that was the point of $num_chars_stripped and the while...

to get past the last word duplicity problem you will need to count how many times the word occurs in the string, then while through the string until you find the word the _blank_ the last time it occurs... ugh... however using last word seems fairly idiotic... i am sorry, my way works best (a conceited fact true... however...) the only way is to parse it by stripping valid chars only and skipping over tags with array pointers as i have mentioned... this task is TOO difficult to do off the top of ones head, it requires you to sit there for an hour or so testing the damn thing.... best of luck to you with that...
Copy linkTweet thisAlerts:
@GS300authorSep 16.2004 — Hello all ,

Hellspire

I think your method is better than last word method ?


But I'm newbie in regular expression

there is a problem in last word method as evil_moose said .


Thanks all ?
×

Success!

Help @GS300 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...