How to get a substring between two strings using regular expression in PHP

2 Answers

0 votes
$s = 'PHP is a general-purpose scripting language [sub]suited to web[/sub] development.';

preg_match_all("~\[sub\](.*?)\[\/sub\]~",$s, $subs);

print_r($subs[1]);
echo $subs[1][0];

  
  
  
/*
run:
  
Array
(
    [0] => suited to web
)
suited to web
  
*/

 



answered Dec 21, 2020 by avibootz
0 votes
$s = 'PHP is a general-purpose scripting [language] suited to web [/development].';
 
preg_match_all("~\[language\](.*?)\[\/development\]~",$s, $subs);
 
print_r($subs[1]);
echo trim($subs[1][0]);

 

  
/*
run:
  
Array
(
    [0] =>  suited to web 
)
suited to web

*/

 



answered Dec 21, 2020 by avibootz
edited Dec 21, 2020 by avibootz
...