Welcome! Log In Create A New Profile

Advanced

[PHP] slicing and dicing strings

Posted by Kirk Bailey 
Kirk Bailey
[PHP] slicing and dicing strings
June 28, 2012 12:20AM
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
admin
RE: [PHP] slicing and dicing strings
June 28, 2012 12:40AM
Read

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


-----Original Message-----
From: Kirk Bailey [mailto:[email protected]]
Sent: Wednesday, June 27, 2012 6:15 PM
To: php-general@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
+-----+
| BOX |
+-----+
think


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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
Re: [PHP] slicing and dicing strings
June 28, 2012 01:40AM
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/
tamouse mailing lists
Re: [PHP] slicing and dicing strings
June 28, 2012 02:50AM
On Wed, Jun 27, 2012 at 5:15 PM, Kirk Bailey <[email protected]> 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
on this list to spend their time providing answers which *are* readily
available with just a bit of google-fu.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
Re: [PHP] slicing and dicing strings
June 28, 2012 03:20AM
"Stuart Dallas" <[email protected]> wrote in message
news:[email protected]..
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
Geoff Shang
Re: [PHP] slicing and dicing strings
June 28, 2012 11:20AM
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
RE: [PHP] slicing and dicing strings
June 28, 2012 11:30AM
-----Original Message-----
From: Geoff Shang [mailto:[email protected]]
Sent: Thursday, June 28, 2012 5:12 AM
To: php-general@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();
I understand you have the manual open and you think you can find the answer
pretty quickly.
Please understand that knowing a little, to advanced features of all
functions will help you significantly.
It will cut down on research time and you have a better understanding of
what function will better fit
the situation at hand. While some people will never fully remember all
syntax of every function it never hurt
anyone in attempting to understand a majority of PHP functionality.

:)



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Curtis Maurand
Re: [PHP] slicing and dicing strings
June 28, 2012 02:10PM
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.
>>
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/



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Shailesh N. Humbad
[PHP] Re: slicing and dicing strings
June 28, 2012 02:50PM
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/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ian
[PHP] Re: slicing and dicing strings
June 28, 2012 03:20PM
> 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
Re: [PHP] slicing and dicing strings
June 28, 2012 07:10PM
On Wed, Jun 27, 2012 at 6:15 PM, Kirk Bailey <[email protected]> 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
Sorry, only registered users may post in this forum.

Click here to login