Re: Catch line indetation
| From: | Christoph M. Becker | Date: | Sat, 29 Oct 2016 09:44:24 +0000 |
| Subject: | Re: Catch line indetation | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325799@lists.php.net to get a copy of this message | ||
On 29.10.2016 at 09:45, Narcis Garcia wrote:
> El 28/10/16 a les 15:15, Christoph M. Becker ha escrit:
>
>> On 28.10.2016 at 13:09, Ashley Sheridan wrote:
>>
>>> On 28 October 2016 12:01:16 BST, Narcis Garcia <informatica@actiu.net> 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' -> ' '
>>>>
>>>> Thanks.
>>>
>>> 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
>>
>> Shouldn't that be
>>
>> ^( *)[^ ]
>>
>> instead?
>
> preg_replace('^( *)[^ ]', '${1}', '
> <table>...</table>');
> Warning: preg_replace(): Unknown modifier ']'
Of course, the pattern has to be enclosed by delimiters, so you could use:
preg_replace('/^( *)[^ ]/', '${1}', '
<table>...</table>')
--
Christoph M. Becker