Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

Read shortcode in laravel using reg expression

Hi. I have a content like below,

Hope you enjoy this quiz. If you like it, then please share it. Thank you. [WpProQuiz 2265]

Here i want fetch only [WpProQuiz 2265] if it matches and get the number 2265.

How to do this?

0 likes
6 replies
CorvS's avatar
CorvS
Best Answer
Level 27

@deekshith You could do the following:

preg_match('/\[WpProQuiz\s(.*?)\]/', '... [WpProQuiz 2265]', $matches)

Then $matches[1] contains the number you want.

1 like
Deekshith's avatar

@corvs Thank you so much it is working fine. i should use the loop if it has more than one shortcode right?

CorvS's avatar

@deekshith No, you can use preg_match_all instead. Then $matches[1] contains an array with the numbers.

1 like

Please or to participate in this conversation.