开发者

Simple regular expression using preg_match

开发者 https://www.devze.com 2023-04-04 00:25 出处:网络
I am trying to retrieve the three digit number aft开发者_JAVA技巧er this word and semi colon. REF: 222

I am trying to retrieve the three digit number aft开发者_JAVA技巧er this word and semi colon.

REF: 222

The code i have below works but its not good because its getting 3digit numbers from the $decoded_message string.

What i really want is only to grab three digit number after the word REF: ###

if (preg_match("([0-9]{3})", $decoded_message, $matches)) {
  echo "Match was found <br />";
  echo "ref = ".$matches[0];

}

Thanks in advance


You can search for REF: [0-9]{3} and then remove the REF: part.

if (preg_match("/REF: [0-9]{3}/", $decoded_message, $matches)) {
  echo "Match was found <br />";
  echo "ref = ".substr( $matches[0], 5 );
}


You may simply replace "REF: " by using

$output = preg_replace("/REF: /","", "REF: 222");

Afterwards, only the number should be contained in $output.

0

精彩评论

暂无评论...
验证码 换一张
取 消