Kirk Bailey | 28 Jun 2012 00:15
Gravatar

slicing and dicing strings

ok, it slices and dices, but how?
in python, to print all but the last char in string FOO, we say
print FOO[:-1]
But this seems to bark like a basset hound in php. Whyfore?

Now tihs tyro is seeking sage words to help me understand this.
RTFM is not sage words; if you don't want to help me please don't 
waste the bandwidth.

Would anyone care to help me understand this without dedicating 4 
precious and in over demand/under supply hours to RTFM?

-- 
end

Very Truly yours,
                  - Kirk Bailey,
                    Largo Florida

                        kniht
                       +-----+
                       | BOX |
                       +-----+
                        think

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

(Continue reading)

admin | 28 Jun 2012 00:32

RE: slicing and dicing strings

Read

http://php.net/manual/en/function.substr.php

-----Original Message-----
From: Kirk Bailey [mailto:kbailey <at> howlermonkey.net] 
Sent: Wednesday, June 27, 2012 6:15 PM
To: php-general <at> lists.php.net
Subject: [PHP] slicing and dicing strings

ok, it slices and dices, but how?
in python, to print all but the last char in string FOO, we say print
FOO[:-1] But this seems to bark like a basset hound in php. Whyfore?

Now tihs tyro is seeking sage words to help me understand this.
RTFM is not sage words; if you don't want to help me please don't waste the
bandwidth.

Would anyone care to help me understand this without dedicating 4 precious
and in over demand/under supply hours to RTFM?

--
end

Very Truly yours,
                  - Kirk Bailey,
                    Largo Florida

                        kniht
                       +-----+
(Continue reading)

Stuart Dallas | 28 Jun 2012 01:29
Gravatar

Re: slicing and dicing strings

WARNING: May contain traces of sarcasm.

On 27 Jun 2012, at 23:15, Kirk Bailey wrote:

> ok, it slices and dices, but how?
> in python, to print all but the last char in string FOO, we say
> print FOO[:-1]
> But this seems to bark like a basset hound in php. Whyfore?

Because it's not valid syntax in PHP.

> Now tihs tyro is seeking sage words to help me understand this.
> RTFM is not sage words; if you don't want to help me please don't waste the bandwidth.

RTFM is the best response to such a simple request. Seriously, how hard is it to do this:
http://bit.ly/MUDL80? This list is here to help you solve problems, not laziness.

> Would anyone care to help me understand this without dedicating 4 precious and in over demand/under
supply hours to RTFM?

If it would really take you 4 hours to find this out via the manual or Google then I think it would be 4 hours well
spent: 3 hours 55 minutes learning how to use the manual / Google effectively, and 5 minutes to then get the
answer you seek.

-Stuart

--

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
(Continue reading)

Jim Giner | 28 Jun 2012 03:06

Re: slicing and dicing strings

"Stuart Dallas" <stuart <at> 3ft9.com> wrote in message 
news:D889B8E1-2EAB-4F36-83CB-8A52B5EC61BC <at> 3ft9.com...
WARNING: May contain traces of sarcasm.
If it would really take you 4 hours to find this out via the manual or 
Google then I think it would be 4 hours well spent: 3 hours 55 minutes 
learning how to use the manual / Google effectively, and 5 minutes to then 
get the answer you seek.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

********
hear hear!  Or is it here here!? 

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Curtis Maurand | 28 Jun 2012 14:07

Re: slicing and dicing strings


he answer to his question is:

print(substr($string, 0,
strlen - 1));

http://www.php.net/manual/en/book.strings.php

Cheers,
Curtis

Stuart Dallas wrote:
> WARNING: May contain traces of sarcasm.
> 
> On 27
Jun 2012, at 23:15, Kirk Bailey wrote:
> 
>> ok, it
slices and dices, but how?
>> in python, to print all but the
last char in string FOO, we say
>> print FOO[:-1]
>>
But this seems to bark like a basset hound in php. Whyfore?
> 
> Because it's not valid syntax in PHP.
> 
>> Now
tihs tyro is seeking sage words to help me understand this.
>>
(Continue reading)

tamouse mailing lists | 28 Jun 2012 02:48
Picon
Gravatar

Re: slicing and dicing strings

On Wed, Jun 27, 2012 at 5:15 PM, Kirk Bailey <kbailey <at> howlermonkey.net> wrote:
> ok, it slices and dices, but how?
> in python, to print all but the last char in string FOO, we say
> print FOO[:-1]
> But this seems to bark like a basset hound in php. Whyfore?

As said, PHP is not Python. Pretty much *nothing* else is Python.

That said, the trope still has legs in PHP:

From <http://us2.php.net/manual/en/function.substr.php> :

   "If start is negative, the returned string will start at the
   start'th character from the end of string."

> Now tihs tyro is seeking sage words to help me understand this.
> RTFM is not sage words; if you don't want to help me please don't waste the
> bandwidth.

RTFM with no hint of where to look is a problem. But the dictum of
"Read here in the manual" will surely be the best thing. You will then
know where to look for future questions.

> Would anyone care to help me understand this without dedicating 4 precious
> and in over demand/under supply hours to RTFM?

You may not have 4 precious and overdemanded/undersupplied hours, but
at some point everyone who can answer this has had to slog through the
manual; we haven't just been lounging in our opulence pouring over the
documentation out of some need to aleviate our boredom. Nor is anyone
(Continue reading)

Geoff Shang | 28 Jun 2012 11:12

Re: slicing and dicing strings

On Wed, 27 Jun 2012, tamouse mailing lists wrote:

> RTFM with no hint of where to look is a problem. But the dictum of
> "Read here in the manual" will surely be the best thing. You will then
> know where to look for future questions.

Personally, I've got better things to do with my time than spend it 
committing the syntax and semantics of every single PHP function to 
memory.  I nearly always have the php.net front page with its function 
search open in a browser whenever I'm writing something more than a few 
lines.  The function search seriously rocks.  Even if I can't remember 
which string function I want, I can search for *any* string function and 
I'll get the list of all of them, along with a quick summary to help me 
find the one I want.

Geoff.

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

admin | 28 Jun 2012 11:28

RE: slicing and dicing strings

-----Original Message-----
From: Geoff Shang [mailto:geoff <at> QuiteLikely.com] 
Sent: Thursday, June 28, 2012 5:12 AM
To: php-general <at> lists.php.net
Subject: Re: [PHP] slicing and dicing strings

On Wed, 27 Jun 2012, tamouse mailing lists wrote:

> RTFM with no hint of where to look is a problem. But the dictum of 
> "Read here in the manual" will surely be the best thing. You will then 
> know where to look for future questions.

Personally, I've got better things to do with my time than spend it
committing the syntax and semantics of every single PHP function to memory.
I nearly always have the php.net front page with its function search open in
a browser whenever I'm writing something more than a few lines.  The
function search seriously rocks.  Even if I can't remember which string
function I want, I can search for *any* string function and I'll get the
list of all of them, along with a quick summary to help me find the one I
want.

Geoff.

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
-----------------------------------------------------------

Geoff,
	The function you are looking for is SUBSTR();
(Continue reading)

Shailesh N. Humbad | 28 Jun 2012 14:44
Picon
Favicon

Re: slicing and dicing strings

On 6/27/2012 6:15 PM, Kirk Bailey wrote:
> ok, it slices and dices, but how?
> in python, to print all but the last char in string FOO, we say
> print FOO[:-1]
> But this seems to bark like a basset hound in php. Whyfore?
>
> Now tihs tyro is seeking sage words to help me understand this.
> RTFM is not sage words; if you don't want to help me please don't waste
> the bandwidth.
>
> Would anyone care to help me understand this without dedicating 4
> precious and in over demand/under supply hours to RTFM?
>

print substr($foo, 0, -1);

You could also do this, but it takes more code:
print substr($foo, 0, strlen($foo) - 1);

You could wrap it as a function like so:
function strchop($string, $num_chars_to_chop_off_end)
{
	return substr($string, 0, -$num_chars_to_chop_off_end);
}

If you have time, the relevant manual page is:
http://www.php.net/manual/en/function.substr.php

--

-- 
PHP General Mailing List (http://www.php.net/)
(Continue reading)

Ian | 28 Jun 2012 15:14
Picon

Re: slicing and dicing strings


> RTFM is not sage words; if you don't want to help me please don't waste
> the bandwidth.

Hi,

Seeing as you are using Windows, why not download the manual as a handy
Windows Help File (CHM), that way you wont waste bandwidth online every
time you get stumped.

http://www.php.net/download-docs.php

I recommend the version with user notes as there are also lots of great
examples / comments included too.

There are also CHM readers for other operating systems (including
mobiles), so no excuse for not Reading The Fine Manual any more.

Regards

Ian
-- 

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Daniel Brown | 28 Jun 2012 19:08
Picon

Re: slicing and dicing strings

On Wed, Jun 27, 2012 at 6:15 PM, Kirk Bailey <kbailey <at> howlermonkey.net> wrote:
> ok, it slices and dices, but how?
> in python, to print all but the last char in string FOO, we say
> print FOO[:-1]
> But this seems to bark like a basset hound in php. Whyfore?

    It is a longer syntax which, in agreement with some folks, I think
could be shortened (there's a feature request you could submit if you
were so inclined), but there's a few methods you could use.  Two of
which are shown below.

<?php
$foo = 'This seems to bark like a basset hound in PHP';

echo $foo{(strlen($foo) - 1)};
echo substr($foo,-1);
?>

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Gmane