How can strpos() and substr() be used to extract text in PHP?
To extract text in PHP using strpos() and substr(), you can first find the position of a specific substring within a string using strpos(), and then use substr() to extract a portion of the string based on the position found. This combination of functions allows you to efficiently extract text from a larger string based on specific criteria.
// Example code to extract text using strpos() and substr()
$text = "This is a sample text for demonstration";
$keyword = "sample";
$pos = strpos($text, $keyword);
if ($pos !== false) {
$extracted_text = substr($text, $pos, strlen($keyword));
echo $extracted_text;
} else {
echo "Keyword not found in the text.";
}