Topic: [Trim/Truncate] Only first word?

I'm using this code to limit the subject to 5 letters (don't ask why wink )

.$subject_truncated = pun_htmlspecialchars(trim(substr($cur_post['subject'], 0, ($max_subject_length=5)))).

But now I want it to display only the first word of the subject-line. Can you help?

2 (edited by Smartys 2006-10-29 15:54)

Re: [Trim/Truncate] Only first word?

.$subject_truncated = pun_htmlspecialchars(trim(substr($cur_post['subject'], 0, strpos($cur_post['subject'], ' ')))).

I *think* that should do it, although I'm not sure about the order of the arguments in strpos()

Edit: I corrected the order of arguments -Smartys
Edit2: Mmm, an underscore somehow got added -Smartys

Re: [Trim/Truncate] Only first word?

It doesn't work...

I don't think strpos() is the correct way to get what I want.

I want this:
Original Subject: "News: blabla blaah blaah?"
to
Trimmed Subject: "News"
So only the first word, which I can then use to define a class of a div.

Re: [Trim/Truncate] Only first word?

Odd, it should work
What does it give you?

Re: [Trim/Truncate] Only first word?

It doesn't output anything...

Re: [Trim/Truncate] Only first word?

I switched the arguments of strpos(). Change them and it should be fine.

Re: [Trim/Truncate] Only first word?

elbekko wrote:

I switched the arguments of strpos(). Change them and it should be fine.

But I edited your post to correct the order tongue
And now it should work, the code had an extra underscore hmm

Re: [Trim/Truncate] Only first word?

Even with the correct order, it doesn't output anything.

Re: [Trim/Truncate] Only first word?

Smartys wrote:

But I edited your post to correct the order tongue
And now it should work, the code had an extra underscore hmm

Didn't notice that tongue

I hate my memory sad

And yes, the code should work...

Re: [Trim/Truncate] Only first word?

Are you sure there isn't another method? I'm not convinced that strpos() is the correct way to do this.

Re: [Trim/Truncate] Only first word?

Fabiaan wrote:

Are you sure there isn't another method? I'm not convinced that strpos() is the correct way to do this.

Why? It makes sense
substr takes from 0 to the return value of strpos. strpos is supposed to return the position of the first occurance of the given string (here, a space).

I mean, you could do something like explode the string using spaces as the delimeter and use the 0th value of the new array, but substr/strpos seems to make more sense

Re: [Trim/Truncate] Only first word?

Smartys wrote:

strpos is supposed to return the position of the first occurance of the given string (here, <b>a space</b>).

I replaced the space with an ":", and now it works!
(Oh, and when I try " " it doesn't work, maybe a sort of character problem?)

Eureka.

Re: [Trim/Truncate] Only first word?

Odd, works fine for me without any editing smile