在PHP中给出这个字符串:
$string = '/sometext?123#abc/moretext';
我如何测试模式“?123#abc /”的存在,它总是被“?”包围和“/”,但有不同的内部文本,可能包括任何文字和符号?模式之外的文本也将不同。我需要这样做:
if ($string includes pattern ?*/) {
//load the inner value into a variable
//then remove the entire patern including the leading "?" and trailing "/" and replace with a single "/"
}
我该怎么做?
答案 0 :(得分:0)
<?php
$string = '/sometext?123#abc/moretext';
$pattern = '/\\?(.*?)\\//';
if( $pieces = preg_split($pattern, $string, Null, PREG_SPLIT_DELIM_CAPTURE)) {
echo($pieces[1] . "\n");
unset($pieces[1]);
echo(implode("/", $pieces) . "\n");
}
?>
--output:--
~/php_programs$ php 1.php
123#abc
/sometext/moretext
答案 1 :(得分:-1)
试试这个
$s = '/sometext?123#abc/moretext';
$matches = array();
$t = preg_match('#\?(.*?)\/#s', $s, $matches);
if($matches[1])
echo "match";
else
echo "not";
match