0

在 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 "/"

}

我该怎么做呢?

4

2 回答 2

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
于 2013-05-11T05:23:58.850 回答
-1

尝试这个

$s = '/sometext?123#abc/moretext';
$matches = array();
$t = preg_match('#\?(.*?)\/#s', $s, $matches);
if($matches[1])
   echo "match";
else
   echo "not";

输出

match

键盘

于 2013-05-11T05:00:06.887 回答