Re: [PHP] Catch line indetation
- Date: Fri, 28 Oct 2016 15:21:19 +0200
- From: "Christoph M. Becker" <cmbecker69@xxxxxx>
- Subject: Re: [PHP] Catch line indetation
On 28.10.2016 at 14:51, Richard wrote:
>> Date: Friday, October 28, 2016 12:09:31 +0100
>> From: Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx>
>> On 28 October 2016 12:01:16 BST, Narcis Garcia
>> <informatica@xxxxxxxxx> wrote:
>>> Hello, I have a string (I quote here only) as:
>>> ' <table>...</table>'
>>> As you can see there are 3 spaces at the beginning, but it could
>>> be 0 or
>>> 4 or any number of spaces.
>>> How can I get a string with only the initial spaces part?
>>> ' <table>...</table>' -> ' '
>>> 'hello' -> ''
>>> ' hello' -> ' '
>> Have you tried regular expressions? Something like:
>> ^( )*[^ ]
>> The first captured match is the number of spaces, from 0 to any
>> amount. Not the space between the brackets and before the closing
>> square bracket
> You need to take into consideration that "whitespace" can be created
> by more than the simple "space" (ascii 32) character. A "[horizontal]
> tab" (ascii 9) is common, but also look at the top of php trim
> function documentation:
> to see the characters that it handles as "whitespace".
If general whitespace should be detected with a regexp, \s could be used.
> While "trim"
> does the opposite of what you're after, […]
Indeed, so one could do something like
substr($string, 0, strlen($string) - strlen(ltrim($string)))
I'd prefer a regexp solution, though.
Christoph M. Becker
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php