Home > Software design >  PHP How can find a string start with an end tag to get the string from inside?
PHP How can find a string start with an end tag to get the string from inside?

Time:10-10

$stringText = 'Example text [1212sdas|4542189789/] continue text...  

My result finally is an array with: ['1212sdas|4542189789']

My text is:

"Example text [1212sdas|4542189789/]..."

My result should be:

array('1212sdas|4542189789'

CodePudding user response:

Use this:

((?<=\[).*?(?=\/\]))

It matches smallest pattern that has [ on the left and \] on the right.

(?<=...) is positive lookback that means what should be on the left. (?=...) is positive lookahead that means what should be on the right. .*? means any smallest pattern that regex could find. otherwise it match all character between the first [ and the last \] .

Demo

  • Related