I have the following code:
<?php
$subject="something";
$pattern="something";
If(preg_match($pattern,$subject)){
echo "Match is found";
}else{
echo "Match is not found";
}
?>
If I have
$subject="apple is fruit and I love apple";
The main point being the string beginning with apple and ending with apple
Then what should be the pattern so that
match is found
is the output.
I know the pattern can be
"/^apple.*apple$/"
But I don't want to repeat the same word apple.
"apple"? Put it in a variable:$word = "apple";, or usesprintfto build your pattern."/^(apple).*(?1)$/s"(due to I don't want to repeat the same word apple.)appleis in fact a placeholder for some other pattern, e.g.<<[A-Z]{3}>>. But not really sure, the question is rather weird. I just do not see any issue. Closing it as primarily opinion-based.