1

I need the text delimited by "~ (Number from 0 to 13)" and ending at "~ end" each position of the array should have the text that is between the braces. Does anybody have an idea ?

TEXT: (The original has a lake text and maybe html)

 ~0 
    aaaaaa1
    aaaaaaaaaa
    ~1 
    bbbbbbbbbb
    sdf23
    324 <br>
    sdfs
    ~2 
    cccccccccc
    ~3 
    ddddddddddd 

    ~13 
    eeeeeeeeeee 

    ~14 
    fffffffffff 
            ~end

END Array:

 Array
                (
                    [0] =>  aaaaaa1
                            aaaaaaaaaa

                    [1] => bbbbbbbbbbb
                            sdf23
                            324 <br>
                            sdfs 

                    [2] => cccccccccc 

                    [3] => dddddddddd
                    .
                    .
                    .
                    .
                    [13] => eeeeeee 

                    [14] => fffffff 


                )

My PHP with regex: (fail)

$texto = "
 ~0 
    123hola321
    yyyyyyyyyyy
    ~1 
    rrrrrrrrrrrr
    sdf23
    324 <br>
    sdfs
    ~2 
    cccccccccc
    ~3 
    ddddddddddd 

    ~13 
    ddddddddddd 

    ~14 
    ddddddddddd 
            ~end  ";


$regex = '/^~(\d{1,2}.\n)(.*?)/m';
echo $regex;
preg_replace($regex,$texto,$matches);


echo "<pre>";
print_r($matches);
echo "</pre>";

//      ^~(\d{1,2}.\n)    

// ~\d{1,2} (.*?)2$
// 
//  ^~\d{1,2}(.*?)end$

thx

1
  • Do preg_split("/~[0-9]+/", $texto); Commented Dec 22, 2016 at 12:27

2 Answers 2

1

I would use preg_split() instead:

$arr = preg_split('/~\d{1,2}/', $texto);

No need to capture everything in between.

Of course this will only work if the keys are sequential and start at 0 or they don't matter.

Edit: If you want to trim the resulting strings in the process, you should not just add any character to the regex, the dot .; this can remove valid characters from your results.

Instead, only remove the white-space with this:

$arr = preg_split('/\s*~\d{1,2}\s*/', $texto);

\s* means 0 to any number of white-space characters (spaces, tabs, newlines, etc.).

Sign up to request clarification or add additional context in comments.

1 Comment

this help me to make a solution. thanks !! Gracias ;)
0
$texto = "
~0 
123hola321
yyyyyyyyyyy
xxxxxxxx
ffffffffff
~1 
rrrrrrrrrrrr
~2 
cccccccccc
~3 
ddddddddddd 

~3 
ddddddddddd 
        ~end  ";


$arr = preg_split('#~\d{1,2}.(\r\n|\n|\r)#', $texto);

echo "<pre>";
print_r($arr);
echo "</pre>";

1 Comment

Please explain what the error was in the OP's attempt, and how your code resolves that error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.