By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

using substr() with apostrophe in the string

P: n/a
Hey folks -

I'm working on a spellchecking routine. The pspell libraries don't
handle punctuation; I have to make a script a little smarter.

I'm having a problem popping English posessive endings off of strings.
I thought that this little snipper would do the job:

<code>
if ( substr( $word, -2) == "\'s" ) {
$word = substr($word, -2);
}
</code>

But it doesn't. I wrote some test code, and it seems that PHP or Apache
or something is parsing the apostrophe as something else, at least for
a few characters. See what mean ( p() is a function that encapsulates a
string in <P> </P> tags):

<code>
echo p( $word . " " . substr($word, -1) );
echo p( $word . " " . substr($word, -2) );
echo p( $word . " " . substr($word, -3) );
echo p( $word . " " . substr($word, -4) );
echo p( $word . " " . substr($word, -5) );
echo p( $word . " " . substr($word, -6) );
echo p( $word . " " . substr($word, -7) );
echo p( $word . " " . substr($word, -8) );
</code>

<output>
guest's s

guest's ;s

guest's 9;s

guest's 39;s

guest's 039;s

guest's #039;s

guest's 's
</output>

So it would seem that

<code>
substr( $word, -7)
</code>

should return 's , but it doesn't. Or, at least, the following does not
do the job:

<code>
if ( substr( $word, -7) == "\'s" ) {
$word = substr($word, -7);
}
</code>

What gives?

Jul 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
la*****@gmail.com wrote:
What gives?


I'm guessing you have the apostrofe in its entity code

You can try and decode the string:

$word = html_entity_decode($word, ENT_QUOTES);
Jul 29 '05 #2

P: n/a

jamen wrote:
la*****@gmail.com wrote:
What gives?


I'm guessing you have the apostrofe in its entity code

You can try and decode the string:

$word = html_entity_decode($word, ENT_QUOTES);


That's exactly right! I was encoding the strings before they were
entered into the function. Thanks.

Jul 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.