Good evening.

Can you tell me how to cut out the first and last characters in a regular season? Let's say there is such a regular season:

preg_match_all('/[\:]{1}[\w\-]{1,}[\:]{1}/i', ':blah-blah-blah-blah:', $print, PREG_SET_ORDER); print_r($print); 

Output:

 Array ( [0] => Array ( [0] => :blah-blah-blah-blah: ) ) 

those. so it is displayed without a colon.

    2 answers 2

    Highlight brackets:

     [\:]{1}([\w\-]{1,})[\:]{1} ^ ^ 

    Then:

     preg_match_all('/[\:]{1}([\w\-]{1,})[\:]{1}/i', ':blah-blah-blah-blah:', $print, PREG_SET_ORDER); print_r($print); 

    It turns out:

     Array ( [0] => Array ( [0] => :blah-blah-blah-blah: ) [1] => Array //то что нужно: ( [0] => blah-blah-blah-blah ) ) 

    NB can be simplified to:

     [\:]([\w\-]+)[\:] 

    {1} - not needed in principle, a {1,} can be replaced by +

    With PHP:

    Use the trim function, it removes the specified characters from the beginning and end of the string.

     trim(':blah-blah-blah-blah:',':') 
    • Thank you very much, I was interested in regulars. - dgd hsk

    In php, there is a forward and backward view :

     /(?<=:)[\w\-]+(?=:)/ 

    [\w\-]+ - the substring that you need,

    (?<=:) - the colon character to the substring (it is not captured in the result),

    (?=:) - the colon character after the substring (it is not captured in the result).

    Work example https://regex101.com/r/M3y0ZK/1

    PS The truth in this form will be such a capture https://regex101.com/r/M3y0ZK/2