Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Example string: This is testing string with \title{test} tags.

If I want to get the content between the parenthesis or brackets I can use below mentioned code.
PHP
$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\[[^\]]*\]/", $text, $matches);
var_dump($matches[0]);

If I want to extract the words between "\title{" and "}",what would be the exact regex?
I tried this code by replacing "[ ]" with "\title{" "}" but failed.
PHP
$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\\title{[^\]]*\}/", $text, $matches);
var_dump($matches[0]);
Posted

1 solution

Try this:

PHP
$text = 'This is testing \title{string} with \title{test} tags.';
preg_match_all('/\\\title\{([\w]+)\}/', $text, $matches);
var_dump($matches);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900