在PHP中给定这个字符串:
$string = '/sometext?123#abc/moretext';
如何测试模式"是否存在?123#abc/"总是由"?"answers"/"包围,但有不同的内部文本,可以包括任何文本和符号?模式之外的文本也会有所不同。我需要这样做:
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 "/"
}
我该怎么做?
<?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
试试这个
$s = '/sometext?123#abc/moretext';
$matches = array();
$t = preg_match('#'?(.*?)'/#s', $s, $matches);
if($matches[1])
echo "match";
else
echo "not";
<标题> 输出match