正則表達式溶液
$string_2 = 'this is the * string I want to display only *';
$pattern = "/(\*)+[\s]*[a-zA-Z\s]*[\s]*(\*)+/";
preg_match($pattern, $string_2, $matches);
echo $matches[0];
PHP字符串函數解決方案,使用:strpos(),strlen的()和SUBSTR()
$string = 'this is the * string I want to display only *';
$findme = '*';
$pos = strpos($string, $findme); // get first '*' position
if ($pos !== FALSE) {
// a new partial string starts from $pos to the end of the input string
$part = substr($string,$pos+1,strlen($string));
// a new partial string starts from the beginning of $part to the first occurrence of '*' in $part
echo substr($part,0,strpos($part, $findme));
}
這是有些低估。如果字符串中包含兩個以上的'*',您希望發生什麼? – Mankarse
好問題!我想那麼它應該返回整個字符串與星星。 – laukok
這是StAckOverflow。它位於每頁的頂部;我不確定你是如何拼錯它兩次的。 :) –