Preg Replace in sub string leaving a part of the string remaining

Advertisements I am using preg_replace I am trying to replace everything between (and ideally including): [BW] and [/BW] with an empty string. $string = MyText-[BW]field[/BW]-[COUNTER]0001[/COUNTER]-[D]YY/MM/DD[/D][BW]field_2[/BW] $string = preg_replace(‘/’.preg_quote(‘[BW]’).'[\s\S]+?’.'[\/BW]’.’/’, ”, $string); However the output I am getting seems to be keeping the follwing part: BW] i.e. MyText-BW]-[COUNTER]0001[/COUNTER]-[D]YY/MM/DD[/D]BW] The result I want is MyText–[COUNTER]0001[/COUNTER]-[D]YY/MM/DD[/D] I have a… Read More Preg Replace in sub string leaving a part of the string remaining

PHP preg_replace to extract first number before dash

Advertisements Many examples are – but still a stumbling block in my mind. I need to extract first number 21538 from links like $input_line =’https://ex.com/prom-nt/21538-asus-screen17.html’ using PHP preg_replace like preg_replace(‘/(^\D+)(.*)-(.*)/’, ‘\2’, $input_line); I get output 21538-asus using PHP preg_replace like preg_replace(‘/(^\D+)(.*)[0-9]-(.*)/’, ‘\2’, $input_line);) I lose last digit (8) and get output 2153 not 21538 –… Read More PHP preg_replace to extract first number before dash