Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
文字列を取得していますが、「続きを読む」という単語の後の文字列の残りを削除したいと考えています。
例えば
$str = '何かについての非常に長い説明。別の Web サイトで何かについて詳しく読んでください';
データの長さが異なる可能性があるため、 substr を使用したくありません。
何か案は?
次のようにstrpos()とを組み合わせることができます。substr()
strpos()
substr()
$firtpart = substr($str, 0, strpos($str,'read more'));
またはexplode()、文字列を 2 つの部分に分割し、最初の部分を取るために使用します。
explode()
list($firtpart) = explode('read more', $str);
または、正規表現を使用することもできます:
$firtpart = preg_replace("/read more.*$/", '', $str);
strstr($str, 'read more', true)