Hi, Please excuse if similar issue has been posted earlier. My data is like I am going [ABCDEF]123[/ABCDEF] to movie. I want a regex to get the value between [ABCDEF] and [/ABCDEF] only if its integer. eg if its [ABCDEF]junk characters[/ABCDEF] then I dont want...but if its [ABCDEF]123[/ABCDEF] then I require 123 Regards
Here you go: The regex: #^\[ABCDEF\]([0-9]*)\[/ABCDEF\]$# <?php $data = <<<DATA [ABCDEF]123[/ABCDEF] DATA; if(preg_match("#^\[ABCDEF\]([0-9]*)\[/ABCDEF\]$#", $data, $matches)) { print($matches[1]); } else { print("No matches found"); } ?> PHP: [ABCDEF]123[/ABCDEF] = is valid [ABCDEF]junk characters[/ABCDEF] = not valid
If you have a multitude of these patterns in your data, remove the ^ and $ from the pattern above and use the preg_match_all() function